KatInterview

Rebuild an Outline Tree from Depth-Tagged Lines

Stacks & QueuesAuto-graded · 21 test cases
MediumAuto-gradedStacks & Queuesstacktree-constructionparsing

Last updated

Turn a flat sequence of (depth, label) entries into a nested tree by attaching each entry to its nearest earlier entry with a smaller depth, using a monotonic stack.

Outline Tree Reconstruction

A document outline is stored as a flat, ordered sequence of entries. Each entry is a pair (depth, label), where depth is a positive integer and label is a string. Your job is to rebuild the hierarchy these entries describe.

Implement build_outline(entries), which returns the outline as a list of top-level nodes. Each node is a dictionary with exactly two keys:

  • "text": the entry's label (a string)
  • "children": a list of child nodes, in the same format, in their original order

Attachment rules

Process the entries in the given order. For each entry with depth d:

  1. Look at the entries that appear before it and find the most recent one whose depth is strictly smaller than d.
  2. If such an entry exists, the current entry becomes the last child of that entry.
  3. If no such entry exists (for example, the sequence starts at depth 2, or the entry is at depth 1), the current entry is a top-level node and is appended to the returned list.
  4. Entries with equal depth are siblings, never parent and child.
  5. Depth jumps are allowed (e.g. from 1 straight to 3). Do not create filler nodes; the entry simply attaches to the nearest qualifying earlier entry.
  6. Both the top-level list and every children list must keep the original input order.

Note that an earlier entry only counts as a candidate by the "most recent with a smaller depth" rule, so once a later entry with a depth less than or equal to it appears, it can no longer be reached as a parent by anything that follows that later entry in a way that would skip the later entry.

Input

  • entries: a list of [depth, label] pairs (integer, string).

Output

A list of nodes of the form {"text": str, "children": [...]}. Return [] for empty input.

Constraints

  • 0 <= len(entries) <= 10^5
  • 1 <= depth <= 10^4
  • label is a non-empty string of length at most 100
  • Target time complexity: O(N)
  • Target extra space: O(N)
  • The nesting can be as deep as N, so avoid relying on recursion.

Examples

Example 1

Input: build_outline([[1, 'Fruit'], [2, 'Apple'], [2, 'Banana'], [3, 'Cavendish'], [1, 'Vegetable']])

Output: [{'text': 'Fruit', 'children': [{'text': 'Apple', 'children': []}, {'text': 'Banana', 'children': [{'text': 'Cavendish', 'children': []}]}]}, {'text': 'Vegetable', 'children': []}]

Example 2

Input: build_outline([[1, 'Title'], [3, 'Deep note'], [2, 'Section']])

Output: [{'text': 'Title', 'children': [{'text': 'Deep note', 'children': []}, {'text': 'Section', 'children': []}]}]

Example 3

Input: build_outline([[2, 'Orphan'], [3, 'Child'], [1, 'Root'], [4, 'Deep']])

Output: [{'text': 'Orphan', 'children': [{'text': 'Child', 'children': []}]}, {'text': 'Root', 'children': [{'text': 'Deep', 'children': []}]}]

Hints

Hint 1

Think of the input as a pre-order walk of a tree where each entry's depth tells you how far down it sits. Which earlier entries could still be a parent of a future entry?

Hint 2

Keep a stack of (depth, node) pairs forming the current chain of possible parents. Before placing a new entry, discard stack items whose depth is greater than or equal to the new depth.

Hint 3

After popping, an empty stack means the entry is top-level; otherwise attach it to the node on top of the stack. Then push the new entry. Use >= when popping so equal depths become siblings.