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 {
			BufferedReader br = new BufferedReader(new FileReader("transactions"));
			
			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[0]); //Date
				Double amount = Double.parseDouble(line[2]);
				transactions.add(new TransactionCompany(line[1], d, amount));
			}
			
			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.

- Arthur December 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PosDataDiscount {

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

String line;

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

String posValues[] = line.split(",");
Triplet<String, String, Integer> t = new Triplet<String, String, Integer>(
posValues[0], posValues[1], Integer.valueOf(posValues[2].trim()));
lst.add(t);
}
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);
}
public PosDataDiscount() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
PosDataDiscount pos = new PosDataDiscount();
try
{
pos.readEachLine();
pos.chargeForEachTransaction();
}catch(Exception e)
{
e.printStackTrace();
}

}
}

- ganesh January 13, 2015 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More