Amazon Interview Coding Question: Most Common 3-Page Visit Sequence

18 Views
No Comments

Given a log of website requests, where each line contains an entry with the following fields (time, customerId, pageVisited), write an algorithm to find the top 3-page sequence of page visits for all customers. Each line represents a request (A-Z) made by customer (C#) at time T to one of the website’s pages.

For example, given the following log file containing:

T0,C1,A
T0,C2,E
T1,C1,B
T1,C2,B
T2,C1,C
T2,C2,C
T3,C1,D
T3,C2,D
T4,C1,E
T5,C2,A

Sequence of visits for each customer:

C1 = A -> B -> C -> D -> E
C2 = E -> B -> C -> D -> A

Answer: We see that the most common 3-page sequence visited by a customer is B -> C -> D.

This problem is a log-processing and frequency-counting task: first group requests by customerId and sort them by time to reconstruct each user's visit history, then enumerate every consecutive 3-page window for each customer and count how often each sequence appears across all users. A hash map is typically used both for grouping and for counting the 3-page patterns, making the solution straightforward and efficient.

END
 0