Amazon Interview Question for Software Engineers


Team: Amazon Alexa
Country: United States
Interview Type: In-Person




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

Implementation in Java8

/**
   *
   * @param d - number of consecutive days
   * @param log - list of lines from the log file
   * @param separator - separator between the date and id
   * @return - set of loyal customers who logs in {@code d} consecutive days
   */
  static Set<String> getLoyalCustomers(int d, String[] log, String separator) {
    Set<String> loyalCustomers = new HashSet<>();
    Map<String, LinkedList<String>> cache = new HashMap<>();
    for (String line : log) {
      String[] details = line.split(separator);
      String day = details[0];
      String id = details[1];
      // to check if the customer is already a loyal one
      if (loyalCustomers.contains(id)) {
        continue;
      }
      // to check if the customer is in cache already
      if (!cache.containsKey(id)) {
        cache.put(id, new LinkedList<>());
        cache.get(id).add(day);
      } else {
        // to check if the day and last logged day is the consecutive days.
        if (isItTwoConsecutiveDays(cache.get(id).getLast(), day)) {
          if (cache.get(id).size() == d - 1) {
            loyalCustomers.add(id);
            cache.remove(id);
          } else {
            cache.get(id).add(day);
          }
        } else {
          cache.get(id).clear();
          cache.get(id).add(day);
        }
      }
    }
    return loyalCustomers;
  }

  /**
   *
   * @param day1 - day in format MM/dd/yyyy
   * @param day2 - day in format MM/dd/yyyy
   * @return - true if day2 is follow by day1, otherwise false
   */
  private static boolean isItTwoConsecutiveDays(String day1, String day2) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate dayOne = LocalDate.parse(day1, formatter);
    LocalDate dayTwo = LocalDate.parse(day2, formatter);
    return dayOne.plusDays(1).isEqual(dayTwo);
  }

required imports

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

- Mike L April 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All you have to do is to maintain a dictionary of customerID and the list od dates.
You read the file line by line and for each line, put an entry into the dictionary.
Now, let's assume that data are sorted in the file by the date.assuming each date are written in the file in increasing order only.

You will start like this.

Public static List<int> GetConsecutiveCustomer(string filepath)
{
	var customeDateMap = new Dictionary<int,List<Date>>();
	var streamReader = new StreamReader(new FileStream(filepath, FileMode.Open, FileAccess.Read), Encoding.UTF8);
	var consecutiveCustomers = new HashSet<int>();
	while((var line = streamReader.ReadLine()) != null)
	{
		var customerId = Int32.Parse(line.Split('\t')[1]);
		var date = Date.Parse(line.Split('\t')[0]);
		if(customeDateMap.ContainsKey(customerId)
		{
			if(customeDateMap[customerId].Length == 3)
			{
				customeDateMap[customerId].RemoveAt(2);
			}
			customeDateMap[customerId].Add(date);
			if(!consecutiveCustomers.Contains(customerId))
			{
				if(customeDateMap[customerId].Length == 3 
				&& customeDateMap[customerId][0] ==  customeDateMap[customerId][1].AddDays(1) 
				&& customeDateMap[customerId][0] == customeDateMap[customerId][2].AddDays(2))
				{
					consecutiveCustomers.Add(customerId);
				}
			}
		}
		else
		{
			customeDateMap.Add(customerId, new List<date>(){date});
		}
	}
	return consecutiveCustomers.ToList<int>();
}

The time complaxity of this Algorithm is O(N) and the space complexity is O(N) only. Where N is is number of lines in the file.

- sonesh April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 This is the one which should do it
*/
def has_3_consecutive( dates ){
  if ( size(dates) < 3 ) return false
  ld = list(dates)
  // in long form 3 consecutive date are (D - d), D, (D + d) 
  exists([2: size(dates)]) where { 2 * ld[$.o -1 ] == ( ld[$.o-2] + ld[$.o] )  } 
}

ms = mset( file( 'logfile.txt' ) ) as { #(date,key) = $.o.split('\t') ; key }
users_cons = select( ms ) where {
  dates = sset ( $.o.value ) as {  #(date,key) = $.o.split('\t') ;  int( time(date,'MM/dd/yyyy') ) } 
  has_3_consecutive( dates )
} as { $.o.key }

- NoOne April 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Set<String> getLoggedInUsersForConsecutiveThreeDays() throws IOException {
		Map<String, Map<Date, Integer>> dataStruc = new HashMap<String, Map<Date, Integer>>();
		Path path = Paths.get("c:\\tmp", "test_cc.txt");
		Set<String> users = new HashSet<String>();
		try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
			for (String line : (Iterable<String>) lines::iterator) {
				StringTokenizer st = new StringTokenizer(line, "\t");
				Date currDate = new Date((String) st.nextElement());
				String user = (String) st.nextElement();
				if (dataStruc.containsKey(user)) {
					Map<Date, Integer> dateDays = dataStruc.get(user);
					Date lastDate = dateDays.keySet().iterator().next();
					int consecutiveDays = dateDays.get(lastDate);
					if (currDate.getTime() - lastDate.getTime() == (24 * 60 * 60 * 1000)) {
						consecutiveDays++;
						dateDays.remove(lastDate);
						dateDays.put(currDate, consecutiveDays);
						if (consecutiveDays >= 3) {
							users.add(user);
						}
					} else {
						dateDays.remove(lastDate);
						dateDays.put(currDate, 1);
					}
					dataStruc.put(user, dateDays);
				} else {
					Map<Date, Integer> dateDays = new HashMap<Date, Integer>();
					dateDays.put(currDate, 1);
					dataStruc.put(user, dateDays);
				}
			}
		}
		return users;
	}

- bhavin April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SQL code:
select CustomerID from (
select
t1.*, count(*) over (partition by CustomerID order by LoggedIn range interval '3' day preceding) as cons3_days
from t1
) where cons3_days>=3

- Sasi April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select CustomerID from (
select
t1.*, count(*) over (partition by CustomerID order by LoggedIn range interval '3' day preceding) as cons3_days
from t1
) where cons3_days>=3

- Sasi April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

asa

- mhannsari May 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what inputs i have to provide?

- mhannsari May 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Public static List<int> GetConsecutiveCustomer(string filepath)
{
var customeDateMap = new Dictionary<int,List<Date>>();
var streamReader = new StreamReader(new FileStream(filepath, FileMode.Open, FileAccess.Read), Encoding.UTF8);
var consecutiveCustomers = new HashSet<int>();
while((var line = streamReader.ReadLine()) != null)
{
var customerId = Int32.Parse(line.Split('\t')[1]);
var date = Date.Parse(line.Split('\t')[0]);
if(customeDateMap.ContainsKey(customerId)
{
if(customeDateMap[customerId].Length == 3)
{
customeDateMap[customerId].RemoveAt(2);
}
customeDateMap[customerId].Add(date);
if(!consecutiveCustomers.Contains(customerId))
{
if(customeDateMap[customerId].Length == 3
&& customeDateMap[customerId][0] == customeDateMap[customerId][1].AddDays(1)
&& customeDateMap[customerId][0] == customeDateMap[customerId][2].AddDays(2))
{
consecutiveCustomers.Add(customerId);
}
}
}
else
{
customeDateMap.Add(customerId, new List<date>(){date});
}
}
return consecutiveCustomers.ToList<int>();
}



how to run it from main method

- mhannsari May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what inputs you provided.

- mhannsari May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

package hashmaps.careercup.amazon;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Sibendu Dey on 4/25/2017.
 */

class Employee  {
    String id;
    Date lastLogin;
    int consecutiveDays;

    static final int SECONDS_IN_DAY = 24*60*60*1000;

    public Employee(String id, Date date) {
        this.id = id;
        lastLogin = date;
    }

    public void incrementConsecutiveDays(Date date)  {
        if ( consecutiveDays == 3)
            return;

        if ( (date.getTime() - lastLogin.getTime()) <= SECONDS_IN_DAY)  {
            consecutiveDays++;
        }
        else    {
            consecutiveDays = 1;
        }
        lastLogin = date;
    }

    public boolean isConsecutive()  {
        return consecutiveDays == 3 ? true : false;
    }

}
public class ConsecutiveLogin {

    public static void main( String args[]) {
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        sc.nextLine();
        HashMap<String , Employee> list = new HashMap<>();
        for ( int i = 0 ; i < input ; i++)  {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            try {
                String line = sc.nextLine();
                System.out.println(line);
                String values[] = line.split("/t");
                String dateValue = values[0];

                Date date = sdf.parse(dateValue);

                String employeeID = values[1];

                Employee employee = null;
                if ( list.containsKey(employeeID))  {
                    employee = list.get(employeeID);
                    employee.incrementConsecutiveDays(date);
                }
                else    {
                    employee = new Employee(employeeID, date);
                    list.put(employeeID , employee);
                    employee.incrementConsecutiveDays(date);
                }

          } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        findEmployeeWithConsecutiveLogin(list);
    }

    private static void findEmployeeWithConsecutiveLogin(HashMap<String, Employee> list) {

        for (Map.Entry<String , Employee > entry : list.entrySet())   {
            Employee employee  = entry.getValue();
            if ( employee.isConsecutive())  {
                System.out.println( entry.getKey());
            }
        }

    }
}

- Sibendu Dey April 25, 2017 | 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