WhatnotCoding·60 minFree preview
User Journey Paths: Action-Log Prefix Tree
Given (user_id, time, action) logs, rebuild each user's time-ordered journey, insert every journey into a trie that counts distinct users per path prefix, and print the trie as an indented tree. Follow-up: logs arrive as a stream. Recruiters may announce two LC mediums, but interviewers often swap in this problem as the only question.
SWE
trie
dfs
hashmap
sorting
streaming
medium
Frequency
Medium
Last asked
2026-11-20
Stage
phone-screen
Problem
Build a summary of every unique action path (prefix) in the activity logs and how many users took it.
- Input:
List[Tuple[user_id: int, time: int, action: str]]. Logs may be out of time order. - Per user, sort actions by
timeto get that user's journey. - Each user's journey contributes once to every prefix it passes through; the count on a node is the number of distinct users who reached it via that path.
- A repeated action is a distinct step (
A -> B -> Bis a valid journey). - Output: an indented string of the path tree; children are prefixed with
->, one level deeper.
def build_summary(logs: list[tuple[int, int, str]]) -> str: ...Examples
logs = [(100, 1000, "A"), (300, 1150, "A"), (200, 1200, "B"), (100, 1200, "B"),
(100, 1300, "C"), (200, 1400, "A"), (300, 1500, "B"), (300, 1550, "B"),
(100, 1560, "D")]
# user 100: A -> B -> C -> D
# user 200: B -> A
# user 300: A -> B -> B
A (2)
-> B (2)
-> C (1)
-> D (1)
-> B (1)
B (1)
-> A (1)logs = [[100, 1, "A"], [101, 2, "A"], [100, 3, "B"], [102, 4, "B"],
[101, 5, "C"], [100, 6, "C"], [103, 7, "A"], [103, 8, "B"]]
A(3)
-> B(2)
-> C(1)
-> C(1)
B(1)Edge cases:
build_summary([])→""build_summary([(1, 100, "X")])→"X (1)"[(1, 100, "A"), (1, 200, "B"), (2, 100, "A"), (2, 200, "B")]→A (2) -> B (2)[(1, 300, "C"), (1, 100, "A"), (1, 200, "B")]→A (1) -> B (1) -> C (1)(sort before inserting)
Hints
Pitfalls
- Several candidates understood the idea but ran out of time before the trie insertion and the DFS printer were both finished, and were rejected. Treat this as a "must fully run" problem: budget time for the output formatter.
- Counting log lines instead of users per prefix overcounts when a user repeats an action at the same depth on another path. Count once per user per node.
- Forgetting to sort per user by time breaks the out-of-order test case.
- Children print order is not fixed in the prompt; confirm whether the interviewer expects insertion order or sorted order before writing the printer.
- The recruiter may describe the round as two LC-medium problems, but the interviewer can choose this single problem instead.
- Same-timestamp actions for one user make the journey ambiguous. Pick and state a tie-break (stable sort keeps input order, or sort by action name); the samples have no ties but real logs often do.
- Sorting all logs globally by time and then splitting by user is fine, but sorting globally without separating users merges different users' journeys into nonsense sequences.
Talking Points
- Group by user before sorting: each journey only makes sense in that user's own chronological order.
- Count every prefix, not only full journeys:
A (2)andA -> B (2)are separate lines, which is what makes this a trie problem. - Use a nested dict instead of a
TrieNodeclass: shorter to write and easy for the interviewer to read. - Print children in insertion order: users are processed in first-seen order, which reproduces the sample (
CbeforeBunderA -> B). Sort only if the interviewer asks for it, and say which order you chose. - State the space bound: at most one node per log entry, so the trie cannot blow up beyond input size.
Follow-ups
Preparation
- Implement
build_summaryend to end in under 25 minutes, including the indented printer, and run all five edge cases above. - Rewrite it as a streaming class with
ingest(user_id, time, action)andrender(), holding auser_id -> current nodemap. - Practice explaining why per-node counts equal the number of distinct users who reached that prefix.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
