KatInterview

Merge Per-User Action Sequences into a Counted Prefix Tree

Trees & GraphsAuto-graded · 19 test cases
MediumAuto-gradedTrees & Graphstriehash-mapstable-sort

Last updated

Group event records by user, order each user's events by time, merge the sequences into a prefix tree with distinct-user counts, and print it in a fixed preorder format.

Counted Prefix Tree of User Sessions

You receive a list of event records, one per action a user performed. Each record is a triple (user_id, timestamp, action) with types (int, int, str). The records are in no particular order.

Write a function summarize_journeys(events) that returns a text rendering of a prefix tree built from every user's sequence of actions.

Building the sequences

  • Collect all records belonging to the same user. Order that user's records by timestamp, smallest first. The resulting list of actions is the user's journey.
  • If two records of one user share a timestamp, they keep the relative order in which they appeared in the input list. Do not break ties by action name.

Building the tree

  • Every node represents an action reached by following a path of actions from the top. A path of actions from the top is a prefix.
  • A node's count is the number of distinct users whose journey begins with exactly that prefix. Each user adds 1 to each prefix of their own journey, once.
  • Matching is done only among the children of the current node. Repeating an action therefore goes one level deeper: the journey A, B, B produces a node B under the node for A, B, which is separate from the node for A, B.
  • Insert journeys in increasing user_id order. Children of a node are kept in the order in which they were first created during these insertions.

Output format

  • Return one string with one line per node, lines joined by "\n" (no trailing newline).
  • A line is ACTION (COUNT), preceded by an indent when the node is not at the top level.
  • A node at depth d (top-level nodes have d = 0) is prefixed with d space characters followed by -> when d > 0. Top-level nodes have no prefix. For example, depth 1 gives -> B (2) and depth 2 gives -> C (1).
  • Nodes are listed in depth-first preorder: a node, then its entire subtree child by child in creation order, then the next sibling.
  • If events is empty, return the empty string "".

Constraints

  • 0 <= len(events) <= 10^5
  • user_id and timestamp are integers; action is a non-empty string.
  • A single journey can be long (up to the number of events), so avoid relying on deep recursion.
  • The same action may appear several times in one journey.

Examples

Example 1

Input: summarize_journeys([[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']])

Output: 'A (2)\n -> B (2)\n -> C (1)\n -> D (1)\n -> B (1)\nB (1)\n -> A (1)'

Example 2

Input: summarize_journeys([])

Output: ''

Example 3

Input: summarize_journeys([[1, 100, 'X']])

Output: 'X (1)'

Example 4

Input: summarize_journeys([[1, 100, 'A'], [1, 200, 'B'], [2, 100, 'A'], [2, 200, 'B']])

Output: 'A (2)\n -> B (2)'

Hints

Hint 1

Group the records per user first, preserving input order inside each group, then sort each group by timestamp only. Python's sort is stable, so ties keep input order.

Hint 2

Treat each journey as a word and insert it into a trie. Look up the next action only among the children of the node you are currently at, so repeated actions create deeper nodes.

Hint 3

Process users in sorted user_id order and keep each node's children in a list (or insertion-ordered dict). For output use an explicit stack, pushing children in reverse so they pop in creation order, and indent with depth spaces plus '-> '.