Find the Most Common Visit Pattern
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates the ability to process timestamped event logs and extract ordered length-3 visit sequences, testing skills in sorting, frequency aggregation, set-based deduplication, combinatorial pattern enumeration, and efficient use of data structures.
Constraints
- 0 <= n <= 200, where n is the number of log records
- len(userId) == len(timestamp) == len(page) == n
- All timestamps are distinct integers
- 1 <= len(userId[i]), len(page[i]) <= 20 for valid indices i
Examples
Input: (["joe","joe","joe","james","james","james","james","mary","mary","mary"], [1,2,3,4,5,6,7,8,9,10], ["home","about","career","home","cart","maps","home","home","about","career"])
Expected Output: ["home", "about", "career"]
Explanation: Joe and Mary both visited the pattern ["home", "about", "career"]. James has different 3-page patterns, so this pattern has the highest distinct-user count of 2.
Input: (["u1","u1","u1","u2","u2","u2"], [1,2,3,4,5,6], ["a","b","c","a","c","b"])
Expected Output: ["a", "b", "c"]
Explanation: User u1 contributes pattern ["a", "b", "c"] and user u2 contributes ["a", "c", "b"]. Both have count 1, so the lexicographically smaller pattern ["a", "b", "c"] is returned.
Input: (["u1","u1","u1","u1","u1","u2","u2","u2"], [5,1,3,2,4,8,6,7], ["a","a","a","b","b","a","a","b"])
Expected Output: ["a", "b", "a"]
Explanation: After sorting by timestamp, u1 visited ["a", "b", "a", "b", "a"] and u2 visited ["a", "b", "a"]. The pattern ["a", "b", "a"] appears multiple times for u1 but counts only once for that user, so its distinct-user count is 2.
Input: (["u1","u1","u2"], [1,2,3], ["x","y","z"])
Expected Output: []
Explanation: u1 has only 2 visits and u2 has only 1 visit, so no user can form a length-3 pattern.
Input: ([], [], [])
Expected Output: []
Explanation: There are no logs, so no valid 3-page pattern exists.
Hints
- Sort the visits by timestamp first, then collect each user's pages in chronological order.
- For each user, generate all unique 3-page combinations with a set so the same user is counted at most once per pattern.