KatInterview

Assemble a Reply Thread Forest from Flat Records

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphstree-constructionhash-mapcycle-detection

Last updated

Convert an unordered flat list of records with parent links into a nested forest, preserving input order and rejecting duplicate ids, missing parents and cycles.

Reply Thread Forest

You are given a flat list of discussion posts. Each post is a dictionary with an id and, optionally, other fields such as text, plus a parent_id that is either the id of the post it replies to or None for a top-level post. Posts can appear in any order, so a reply may come before the post it replies to.

Write a function that converts this list into a forest of nested posts and returns the list of top-level posts.

Output format

  • Each output post is a copy of the input dictionary with every original field kept, plus a new key children. Its value is a list of the output posts that reply directly to it.
  • Children appear in the same relative order as they do in the input list.
  • The returned list holds the top-level posts (those whose parent_id is None, or whose parent_id key is missing), also in input order.
  • The input dictionaries must not be modified.
  • An id or parent_id of 0 is a real id. Only None means "no parent".

Invalid input

Raise ValueError in these situations:

  1. Two posts share the same id.
  2. A post's parent_id is not None and does not match the id of any post in the list (a dangling reference).
  3. The parent links form a cycle, including a post that is its own parent. Such posts would otherwise never be reachable from any top-level post.

If the input has several problems, check in this order: duplicate ids, then dangling references, then cycles.

An empty list returns an empty list.

Constraints

  • The number of posts N satisfies 0 <= N <= 10^5.
  • Aim for O(N) time.
  • Threads can be very deep (thousands of levels), so avoid recursion in your construction and validation.
  • Ids are hashable values (typically integers).

Examples

Example 1

Input: build_reply_forest([{'id': 2, 'parent_id': 1}, {'id': 1, 'parent_id': None}, {'id': 3, 'parent_id': 1}])

Output: [{'id': 1, 'parent_id': None, 'children': [{'id': 2, 'parent_id': 1, 'children': []}, {'id': 3, 'parent_id': 1, 'children': []}]}]

Example 2

Input: build_reply_forest([{'id': 1, 'text': 'A', 'parent_id': None}, {'id': 2, 'text': 'B', 'parent_id': 1}, {'id': 3, 'text': 'C', 'parent_id': 2}])

Output: [{'id': 1, 'text': 'A', 'parent_id': None, 'children': [{'id': 2, 'text': 'B', 'parent_id': 1, 'children': [{'id': 3, 'text': 'C', 'parent_id': 2, 'children': []}]}]}]

Hints

Hint 1

Create an output node for every post before linking anything, so a reply that appears before its parent causes no trouble.

Hint 2

After indexing, loop over the input in order and append each node to its parent's children list (or to the roots list). This keeps sibling order the same as in the input.

Hint 3

Each post has at most one parent, so cycles can be found by walking up parent links with three states (unvisited, on current walk, known safe). Do this iteratively; reaching a node that is on the current walk means a cycle.