Most common website visit pattern
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round, last asked May 2026. It tests hash maps, sorting and combinatorics.
You get three lists of the same length: username[i] visited website[i] at timestamp[i]. A pattern is a sequence of three websites. A user matches a pattern if they visited those three sites in that order, not necessarily one right after another. Return the pattern matched by the most users; break ties by the lexicographically smallest pattern.
Interface
Unless you agree on a different interface with the interviewer, assume:
most_visited_pattern(username: list[str], timestamp: list[int], website: list[str]) -> list[str]
Example
username = ["joe", "joe", "joe", "james", "james", "james", "james", "mary", "mary", "mary"]
timestamp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
website = ["home", "about", "career", "home", "cart", "maps", "home", "home", "about", "career"]
most_visited_pattern(...) -> ["home", "about", "career"] # joe and mary
username = ["ua", "ua", "ua", "ub", "ub", "ub"]
timestamp = [1, 2, 3, 4, 5, 6]
website = ["a", "b", "a", "a", "b", "c"]
most_visited_pattern(...) -> ["a", "b", "a"] # one user each; a site may repeat in a pattern
Constraints
-
3 <= n <= 50; timestamps are distinct; at least one user has three visits.
Clarifying questions
- Can a pattern repeat a website?
- Does one user count once per pattern even if they match it several ways?
- How are ties broken?
- Are the inputs sorted by time?
What a strong answer covers
- Sort visits by timestamp, group by user, then build each user's set of 3-site combinations so a user counts once.
- A counter over patterns; pick by
(-count, pattern). - O(n log n + Σ C(visits per user, 3)) time, and why that blows up for heavy users.
- Tests for ties and repeated sites.
Follow-ups
- Patterns of length
k. - Only consecutive visits count, or visits within 30 minutes of each other.
- Millions of users in logs: compute it as a batch job.
Where it's asked
- Amazon · Coding · Last asked May 202655 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
