## Accenture Interview Question for Applications Developers

Team: Java Development
Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
0
of 0 vote

I thought of this problem like this.

Construct a class to store the transactions.

Your class should have the following attributes:

``````String company; // to store the name of the company.
int month; //to store the month that the transaction belongs to
Double amount; // to store the amount of the transaction in Dollars or Rupees
Double charge; // to store the charge associated with this transaction``````

So here is how I implemented the TransactionCompany class...

``````import java.util.Calendar;
import java.util.Date;

public class TransactionCompany {

private String company;

private Date date;

private int month;

private Double amount;

private double charge;

public TransactionCompany(String company, Date date, double amount) {
super();
this.company = company;
this.date = date;

Calendar cal = Calendar.getInstance();
cal.setTime(date);

this.month = cal.get(Calendar.MONTH);

this.amount = amount;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}

public double getCharge() {
return charge;
}

public void setCharge(double charge) {
this.charge = charge;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((company == null) ? 0 : company.hashCode());
result = prime * result + month;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TransactionCompany other = (TransactionCompany) obj;
if (company == null) {
if (other.company != null)
return false;
} else if (!company.equals(other.company))
return false;
if (month != other.month)
return false;
return true;
}

@Override
public String toString() {
return "TransactionCompany [company=" + company + ", month=" + (month+1)
+ ", amount=" + amount + ", charge=" + charge + "]";
}

}``````

The logic of the implementation uses two HashMaps to store numbers of free charges given in a month
and the current total of transactions for a company.

That`s the reason why we should @Override hashCode and equals methods to be able to store the information in the hash maps correctly.

A hash for this class is the combination of the company and the month.

Then you can implement a method that takes as as input a List<TransactionCompany> and applies the rules specified to compute the charges for each transaction.

``````public static List<TransactionCompany> processTransactions(
List<TransactionCompany> transactions) {

HashMap<TransactionCompany, Double> totals = new HashMap<TransactionCompany, Double>();
HashMap<TransactionCompany, Integer> bonusGiven = new HashMap<TransactionCompany, Integer>();

for (TransactionCompany t : transactions) {

if (!totals.containsKey(t))
totals.put(t, new Double(0));

if (!bonusGiven.containsKey(t))
bonusGiven.put(t, 0);

if (bonusGiven.containsKey(t) && bonusGiven.get(t) < 2 && t.getAmount() <= new Double(5000) ) {
t.setCharge(0);
bonusGiven.put( t, bonusGiven.get(t) + 1 );
continue;
}

if (totals.containsKey(t) && totals.get(t) >= new Double(50000.00)) {
t.setCharge(0.005 * t.getAmount());
} else {
if (t.getAmount() < new Double(5000)) {
t.setCharge( Math.ceil(0.02 * t.getAmount()) );
totals.put(t, totals.get(t) + t.getAmount());
} else if (t.getAmount() >= new Double(5000)	&& t.getAmount() < new Double(10000)) {
t.setCharge( Math.ceil( 0.015 * t.getAmount()) );
totals.put(t, totals.get(t) + t.getAmount());
} else if (t.getAmount() >= new Double(10000)) {
t.setCharge( Math.ceil(0.01 * t.getAmount()) );
totals.put(t, totals.get(t) + t.getAmount());
} else {
// Amount is negative?? Assuming charge = 0.
t.setCharge(new Double(0));
}
}

}``````

A driver program to read input from a file with the transactions would look like this:

``````public static void main(String[] args) {
try {

int recordsCount = Integer.parseInt( br.readLine().trim() );

List<TransactionCompany> transactions = new ArrayList<TransactionCompany>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

String lineStr = new String();
while((lineStr = br.readLine()) != null) {
String[] line = lineStr.split(",");
Date d = df.parse(line); //Date
Double amount = Double.parseDouble(line);
}

br.close();

processTransactions(transactions);

for(TransactionCompany t: transactions) {
System.out.println(t);
}

} catch (Exception e) {
System.out.println("Error while processing file. Can't calculate transactions.");
}

}``````

I figured the Time complexity is O(n) where n is the number of records in the file and space complexity is O(n) too because you have to store two aditional hash maps that for the worst case scenario would have the same size of the number of records.

Comment hidden because of low score. Click to expand.
0
of 0 vote

private List<Triplet<String,String,Integer>> lst = new ArrayList<>();
{
File file = new File("C:\\Users\\zaloni422\\Desktop\\pig\\POSData");
try {

String line;

while ((line = br.readLine()) != null) {

String posValues[] = line.split(",");
Triplet<String, String, Integer> t = new Triplet<String, String, Integer>(
posValues, posValues, Integer.valueOf(posValues.trim()));
}
br.close();

} catch (IOException e) {
e.printStackTrace();
}
}
public void chargeForEachTransaction() throws ParseException
{
Map<String,String> map = new HashMap<>();
for (Triplet t : lst) {

int trans_amt = 0;
Integer amount = (Integer) t.getValue2();
String month = (String) t.getValue0();
String merchantName = (String) t.getValue0();
if(map.containsKey(merchantName))
{
boolean sameMonthExists = checkForSameMonth(map,month);
if(sameMonthExists)
{
trans_amt = (int) 0.0;

}
}else
{
map.put(merchantName, month);
}
if (amount < 5000) {
trans_amt = (int) (amount * .002);

} else if (amount > 5000 && amount < 9999) {
trans_amt = (int) (amount * .015);
} else if (amount >= 10000) {
trans_amt = (int) (amount * .01);
}
System.out.println("trans_amt-------"+trans_amt);
}
}

private boolean checkForSameMonth(Map<String, String> map, String month) throws ParseException {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date1 = format.parse(month);
Date date2 = format.parse(map.get(month));

cal1.setTime(date1);
cal2.setTime(date2);
return cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH);
}
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
try
{
pos.chargeForEachTransaction();
}catch(Exception e)
{
e.printStackTrace();
}

}
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5755642,"math_prob":0.9120246,"size":3178,"snap":"2020-10-2020-16","text_gpt3_token_len":737,"char_repetition_ratio":0.10617518,"word_repetition_ratio":0.018648019,"special_character_ratio":0.2674638,"punctuation_ratio":0.18532111,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.970425,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-02T00:47:54Z\",\"WARC-Record-ID\":\"<urn:uuid:d4d1197c-1341-43a8-b97e-4660789440a8>\",\"Content-Length\":\"39136\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:14abeb13-6b99-4fc3-a352-ce38c764fef6>\",\"WARC-Concurrent-To\":\"<urn:uuid:58eccf6b-7cec-4c82-b5a8-ba7a18db8f5a>\",\"WARC-IP-Address\":\"216.239.34.21\",\"WARC-Target-URI\":\"https://careercup.com/question?id=5704970600972288\",\"WARC-Payload-Digest\":\"sha1:A7VWW5I7QXSWL3R2PBHFARCDJZE3LHIG\",\"WARC-Block-Digest\":\"sha1:K3UXYAMS6HZE5R5PLKX3PNPU3XFDFUTU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370506477.26_warc_CC-MAIN-20200401223807-20200402013807-00349.warc.gz\"}"}