KatInterview

Prefix-Compressed Trie for Integer Sequences

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designradix-treetriedata-structure-design

Last updated

Simulate operations on a prefix-compressed radix tree storing integer sequences, supporting sequence insertions and depth-first tree serialization.

Design and process a sequence of operations on a compact prefix tree (radix tree) designed to store non-empty sequences of non-negative integers.

Each edge in the radix tree is associated with a non-empty list of integers. The tree maintains two structural rules at all times:

  1. Unique Branching: No two outgoing edges from the same node start with the same integer.
  2. Maximal Compression: Edges extend as far as possible without splitting, diverging only where sequences split or where an inserted sequence ends.

You are given two lists representing a sequence of operations:

  • operations: A list of strings, where each element is either "insert" or "dump".
  • arguments: A list of argument lists corresponding to each operation:
    • For "insert", arguments[i] is a list containing a single integer sequence, e.g., [[1, 2, 3]].
    • For "dump", arguments[i] is empty, i.e., [].

Behavior of Operations

  • insert: Inserts an integer sequence into the radix tree.

    • Traversal begins at the root. At the current node, look for an existing child edge whose label begins with the next remaining integer of the insertion sequence.
    • No matching child: Attach a new child node with an edge label equal to all remaining integers, then complete the operation.
    • Full label match: If the sequence matches the child's edge label completely, advance down to that child node and continue with the remaining suffix of the sequence. If the sequence ends exactly at that child node, the operation completes without modifying the tree.
    • Partial label match: If the sequence matches only a non-empty proper prefix of the child's edge label (or if the inserted sequence ends inside the label), split the edge:
      1. Replace the existing child edge with a new intermediate node labeled with the shared prefix.
      2. Attach the original child (holding its existing subtree intact) to the intermediate node with an edge labeled by the remaining suffix of its original edge label.
      3. If the inserted sequence has additional integers beyond the common prefix, attach a second new child to the intermediate node containing those remaining integers.
    • Re-inserting an already present sequence or inserting a sequence that is a prefix/extension of an existing sequence must maintain the structural invariants.
    • Returns None (null).
  • dump: Returns a snapshot of the tree structure formatted as a list of strings.

    • Performs a depth-first traversal starting from the root (the root node itself is unlabeled and is not included in the output).
    • Each visited non-root node generates one string: its edge label formatted as standard Python list string output (e.g., "[1, 2, 3]"), prepended by 2 space characters (" ") for each level of depth below the root (depth 0 for direct children of the root).
    • At every node, traverse children in ascending order of the first integer of their edge labels.
    • Returns a list of formatted strings.

Return a list containing the result of each operation in the order provided.

Constraints

  • $1 \le \text{len(operations)} \le 10^4$
  • $1 \le \text{length of each inserted sequence} \le 10^3$
  • $0 \le \text{each integer} \le 10^9$
  • Total integers across all inserted sequences $\le 10^5$.

Examples

Example 1

Input: radix_cache(['insert', 'insert', 'insert', 'dump'], [[[10, 20]], [[1, 2, 3]], [[1, 2, 3, 4, 5, 6]], []])

Output: [None, None, None, ['[1, 2, 3]', ' [4, 5, 6]', '[10, 20]']]

Example 2

Input: radix_cache(['insert', 'insert', 'insert', 'insert', 'insert', 'dump'], [[[10, 20]], [[1, 2, 3]], [[1, 2, 3, 4, 5, 6]], [[1, 2, 3, 40, 50, 60, 70, 80]], [[1, 2, 3, 40, 50, 60, 700, 800]], []])

Output: [None, None, None, None, None, ['[1, 2, 3]', ' [4, 5, 6]', ' [40, 50, 60]', ' [70, 80]', ' [700, 800]', '[10, 20]']]

Hints

Hint 1

Use a node representation where children are indexed by the first integer of their edge label to achieve O(1) branching lookup.

Hint 2

When splitting an edge, make sure to preserve the entire subtree attached to the original node under the new split suffix label.

Hint 3

When dumping the structure, sort the children by their initial element before performing depth-first search, and maintain an explicit recursion/stack depth for indentation.