Amazon Vancouver | Phone | Filter logs

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-03-24Added By Anonymous
Source: Link

Company: Amazon
Role: SDE I / SDE II
Interview Stage: Phone Screen
Location: Vancouver
Year: unknown

Original Post Content:

For the below logs (timestamp, customer_id, page_id): Find the most page transition happened. Expected output: *"search->results"* (happened two times).
* 1, Max, Login
* 2, Max, Account
* 3, Max, Search
* 4, Max, Results
* 5, Karl, Search
* 6, Max, History
* 7, Karl, Results


I didn't proceed, although the problem is clear and straightforward. I am suspecting three reasons here:
1. I used HashMap and not concurrentHashMap, because logs could stream in the same moment.
2. I solved the problem for only two transitions, and not generic (like 3 or 4).
3. I didn't use the timestamp at all, but I asked the interviewer if the logs are ordered and they said yes.

I will appreciate your opinions about my mistake(s) here. Thank you.


Here is my solution:

    class Log{
        public int timestamp;
        public String customerID;
        public String pageID;

        public Log(int timestamp, String customerID, String pageID) {
            this.timestamp = timestamp;
            this.customerID = customerID;
            this.pageID = pageID;
        }
    }

    class PageTransition{
        public String from;
        public String to;
        public PageTransition(String from, String to) {
            this.from = from;
            this.to = to;
        }
    }


    public static PageTransition getHigherLogs(List<Log> logs) {
        
        Map<String, List<String>> customerMap = new HashMap<>();
        Map<PageTransition, Integer> pagesCount = new HashMap<>();
        Map<PageTransition, HashMap<String, Integer>> customerCount = new HashMap<>();
        
        int max = -1;
        PageTransition result;
        
        for(Log log : logs)
        {
            if(customerMap.containsKey(log.customerID))
            {
                customerMap.get(log.customerID).add(log.pageID);
            }
            else
            {
                List<String> list = new ArrayList<String>();
                list.add(log.pageID);
                customerMap.put(log.customerID, list);
            }
        }
        
        
        for(Map.Entry<String, List<String>> entry: customerMap.entrySet())
        {
            String customer = entry.getKey();
            List<String> list = entry.getValue();
            for(int i=0; i<list.size()-1; i++)
            {
                PageTransition page = new PageTransition(list.get(i), list.get(i+1));
                pagesCount.put(page, pagesCount.getOrDefault(page,0)+1);
                
                if(!customerCount.containsKey(page.toString())) {
                    HashMap<String, Integer> tempMap = new HashMap<>();
                    tempMap.put(customer, pagesCount.get(page));
                    customerCount.put(page, tempMap);
                }
                else {
                    customerCount.get(page).put(customer, pagesCount.get(page.toString()));
                }
                
                
                if(pagesCount.get(page) > max)
                {
                    max = pagesCount.get(page);
                    result = page;
                }
            }
        }
    }
Comments