Count User Journey Prefixes
Company: Whatnot
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
Quick Answer: This question evaluates the ability to reconstruct per-user action sequences from timestamped logs, aggregate and count path prefixes, and apply data-structure and algorithmic reasoning within the Coding & Algorithms domain.
Constraints
- 0 <= len(logs) <= 100000
- user_id is an integer
- timestamp is an integer
- action is a non-empty string
- If a user's records have the same timestamp, their input order is used as the tie-breaker
- Let L be the total number of action strings across all returned prefixes; test data keeps L <= 200000
Examples
Input: ([[100, 1, "A"], [101, 2, "A"], [100, 3, "B"], [102, 4, "B"], [101, 5, "C"], [100, 6, "C"], [103, 7, "A"], [103, 8, "B"]],)
Expected Output: [[["A"], 3], [["A", "B"], 2], [["A", "B", "C"], 1], [["A", "C"], 1], [["B"], 1]]
Explanation: The journeys are 100: A,B,C; 101: A,C; 102: B; 103: A,B. Their prefixes produce the listed counts.
Input: ([],)
Expected Output: []
Explanation: There are no users and therefore no prefixes.
Input: ([[1, 10, "X"], [1, 10, "Y"], [2, 5, "X"], [2, 6, "X"], [1, 9, "Z"]],)
Expected Output: [[["X"], 1], [["X", "X"], 1], [["Z"], 1], [["Z", "X"], 1], [["Z", "X", "Y"], 1]]
Explanation: User 1's journey is Z,X,Y. The two records at timestamp 10 keep input order: X then Y. User 2's journey is X,X.
Input: ([[5, 3, "C"], [5, 1, "A"], [5, 2, "B"], [6, 2, "A"], [6, 1, "B"], [7, 1, "A"]],)
Expected Output: [[["A"], 2], [["A", "B"], 1], [["A", "B", "C"], 1], [["B"], 1], [["B", "A"], 1]]
Explanation: The logs are not globally sorted. User 5 has A,B,C; user 6 has B,A; user 7 has A.
Input: ([[1, 2, "view"], [1, 1, "login"], [2, 1, "login"], [2, 2, "view"], [3, 1, "login"]],)
Expected Output: [[["login"], 3], [["login", "view"], 2]]
Explanation: Three users start with login, and two of those users continue with view.
Hints
- Group log records by user first, then sort each user's records by timestamp.
- A trie is a natural way to count how many journeys pass through each prefix; a preorder traversal of sorted children can produce the required sorted list.