Amazon Interview Question
Java DevelopersCountry: United States
Interview Type: Written Test
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
public class CustomerLoginInfo {
// Note: We have assumed that customerID are Inetger
// Also the file is read in the custLoginMap
// custLoginMap is Map which contains the Set of customerID logeed in a
// particular Date
public Set<Integer> get4ConsecutiveDays(Map<Date, Set<Integer>> custLoginMap) {
Set<Integer> retSet = new HashSet<Integer>();
// Get the sorted keys
SortedSet<Date> sortedDays = new TreeSet<Date>(custLoginMap.keySet());
Date[] sortedDaysArr = sortedDays.toArray(new Date[sortedDays.size()]);
Set<Integer> s0 = custLoginMap.get(sortedDaysArr[0]);
Set<Integer> s1 = custLoginMap.get(sortedDaysArr[1]);
Set<Integer> s2 = custLoginMap.get(sortedDaysArr[2]);
Set<Integer> s3 = custLoginMap.get(sortedDaysArr[3]);
Set<Integer> s0Ints1 = new HashSet<Integer>(s0);
s0Ints1.retainAll(s1);
Set<Integer> s2Ints3 = new HashSet<Integer>(s2);
s2Ints3.retainAll(s3);
Set<Integer> finalSetInt = new HashSet<>(s0Ints1);
finalSetInt.retainAll(s2Ints3);
retSet.addAll(finalSetInt);
for (int i = 4; i < sortedDays.size(); i++) {
// Destroy s0Ints1
if (i % 4 == 0) {
s0Ints1 = custLoginMap.get(sortedDaysArr[i]);
s0Ints1.retainAll(custLoginMap.get(sortedDaysArr[i - 4 + 1]));
}
// Destroy s0Ints1
if (i % 4 == 1) {
s0Ints1 = custLoginMap.get(sortedDaysArr[i]);
s0Ints1.retainAll(custLoginMap.get(sortedDaysArr[i - 1]));
}
if (i % 4 == 2) {
s2Ints3 = custLoginMap.get(sortedDaysArr[i]);
s0Ints1.retainAll(custLoginMap.get(sortedDaysArr[i - 4 + 1]));
}
if (i % 4 == 3) {
s2Ints3 = custLoginMap.get(sortedDaysArr[i]);
s0Ints1.retainAll(custLoginMap.get(sortedDaysArr[i - 1]));
}
finalSetInt = new HashSet<>(s0Ints1);
finalSetInt.retainAll(s2Ints3);
retSet.addAll(finalSetInt);
}
return retSet;
}
public static void main(String[] args) {
}
}
I would follow these steps:
1. parse the input to collection of class Customer {id, List<date> loginHistory} i.e. List<Customer> customers
2. filter out customers which have loginHistory count less than 4 (and if its exact four logins then filter out customers with more than 4 logins), we dont want to process such records
3. foreach customer, sort loginHistory dates by ascending
4. for each loginHistory date, check if diff of previous date and current date is 1, if it is 1 then take next date and do same comparison until diff is 1, where diff exceeds 1 discard that customer
5. this results customer(s) with consecutive four logins
I would follow these steps:
1. parse the input to collection of Customer objects where Customer {id, loginHistory}
2. filter Customers which has logged in for more than 4 times (or exactly 4 times, depending upon requirement)
3. for each filtered customer
3.1 sort the login history dates
3.1.1 for each login history date
3.1.1.1. subtract previous date from current date
3.1.1.1.1 if result is 1 i.e. diff is 1 day, repeat step above step for next date, until diff is 1 day
3.1.1.1.2 if result is not 1 day, then discard that customer and repeat from 3.1.1
4. result will be those customers which logged in on consecutive days for more than 4 days (or exactly 4 days)
I would follow these steps:
1. parse the input to collection of Customer objects where Customer {id, loginHistory}
2. filter Customers which has logged in for more than 4 times (or exactly 4 times, depending upon requirement)
3. for each filtered customer
3.1 sort the login history dates
3.1.1 for each login history date
3.1.1.1. subtract previous date from current date
3.1.1.1.1 if result is 1 i.e. diff is 1 day, repeat step above step for next date, until diff is 1 day
3.1.1.1.2 if result is not 1 day, then discard that customer and repeat from 3.1.1
4. result will be those customers which logged in on consecutive days for more than 4 days (or exactly 4 days)
test.txt File contents
02/11/2017 0002
11/11/2017 0004
09/11/2017 0004
04/11/2017 0005
04/11/2017 0007
05/11/2017 0002
10/11/2017 0004
01/11/2017 0002
02/11/2017 0003
03/11/2017 0003
04/11/2017 0003
public class CheckConsecutiveDays
{
int numberOfDays = 0;
List<DateTime> date = new List<DateTime>();
Dictionary<string, List<DateTime>> records = new Dictionary<string, List<DateTime>>();
public CheckConsecutiveDays(string fileName)
{
try
{
string line;
using (StreamReader sr = new StreamReader(fileName))
{
while ((line = sr.ReadLine()) != null)
{
string[] str = line.Split('\t');
if (records.ContainsKey(str[1]))
{
records[str[1]].Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
}
else
{
List<DateTime> temp = new List<DateTime>();
temp.Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
records.Add(str[1].ToString(), temp);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
public void PrintCustomerIDList()
{
foreach (string key in records.Keys)
{
int count = 0;
string startDate = "", endDate = "";
records[key].Sort();
for (int i = 0; i < records[key].Count; i++)
{
if (i == 0)
{
count++;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
int val = (Convert.ToDateTime(records[key][i]).CompareTo(Convert.ToDateTime(records[key][i - 1])));
val = Convert.ToInt32((records[key][i].Date - (records[key][i - 1].Date)).TotalDays);
if (((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1) || ((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1))
{
count++;
endDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
count = 1;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
}
if (count > 2)
{
Console.WriteLine("Key: " + key + " Start Date: " + startDate + " End Date: " + endDate);
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
CheckConsecutiveDays ccd = new CheckConsecutiveDays("test.txt");
ccd.PrintCustomerIDList();
}
}
}
Hope it helps :)
test.txt
02/11/2017 0002
11/11/2017 0004
09/11/2017 0004
04/11/2017 0005
04/11/2017 0007
05/11/2017 0002
10/11/2017 0004
01/11/2017 0002
02/11/2017 0003
03/11/2017 0003
04/11/2017 0003
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
namespace PracticeQuestions
{
public class CheckConsecutiveDays
{
int numberOfDays = 0;
List<DateTime> date = new List<DateTime>();
Dictionary<string, List<DateTime>> records = new Dictionary<string, List<DateTime>>();
public CheckConsecutiveDays(string fileName, int noOfDays)
{
numberOfDays = noOfDays;
try
{
string line;
using (StreamReader sr = new StreamReader(fileName))
{
while ((line = sr.ReadLine()) != null)
{
string[] str = line.Split('\t');
if (records.ContainsKey(str[1]))
{
records[str[1]].Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
}
else
{
List<DateTime> temp = new List<DateTime>();
temp.Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
records.Add(str[1].ToString(), temp);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
public void PrintCustomerIDList()
{
foreach (string key in records.Keys)
{
int count = 0;
string startDate = "", endDate = "";
records[key].Sort();
for (int i = 0; i < records[key].Count; i++)
{
if (i == 0)
{
count++;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
int val = (Convert.ToDateTime(records[key][i]).CompareTo(Convert.ToDateTime(records[key][i - 1])));
val = Convert.ToInt32((records[key][i].Date - (records[key][i - 1].Date)).TotalDays);
if (((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1) || ((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1))
{
count++;
endDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
count = 1;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
}
if (count > numberOfDays)
{
Console.WriteLine("Key: " + key + " Start Date: " + startDate + " End Date: " + endDate);
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
CheckConsecutiveDays ccd = new CheckConsecutiveDays("test.txt", 2);
ccd.PrintCustomerIDList();
}
}
}
public static void main(String[] args) {
String s = "04/11/2017 \t 0003 04/12/2017 \t 0003 04/13/2017 \t 0004 04/13/2017 \t 0003 04/13/2017 \t 0003 04/15/2017 \t 0004";
String[] strings = s.replaceAll(" \t","").split(" ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i=0; i<strings.length; i+=2){
String key = strings[i] + "*" + strings[i+1];
if (!map.containsKey(key)){
map.put(strings[i] + "*" + strings[i+1], new Integer(1));
}
else {
map.put(key, map.get(key) + 1);
}
}
System.out.println(map.toString());
}
- Anonymous May 14, 2017