KatInterview

Gather Unique Items Across a Linked Collection Graph

Trees & GraphsAuto-graded · 16 test cases
MediumAuto-gradedTrees & Graphsbfsgraphhash-set

Last updated

Run a breadth-first traversal over a directed graph of folders, emitting each item once in first-seen order, while validating every reference and handling cycles.

Gather Unique Items Across a Linked Collection Graph

A catalog is organized into collections. Each collection holds a list of items and may point to other collections. These pointers form a directed graph, which may contain cycles, self-references, and collections reachable by several routes.

You are given collections, a dictionary mapping a collection ID to a record, and a root_id. Each record is a dictionary with two keys:

  • "pins": a list of item IDs (strings) stored in that collection.
  • "children": a list of collection IDs (strings) that the collection points to.

Return the list of item IDs in the order in which they are first encountered during a breadth-first traversal that starts at root_id.

Traversal rules

  1. Use a standard FIFO queue that initially contains only root_id.
  2. When a collection is taken from the queue, first go through its "pins" in list order, then go through its "children" in list order and enqueue them.
  3. Each collection is processed at most once. Mark a collection as visited at the moment it is enqueued (the root is marked before the loop starts), so cycles and shared children never cause duplicate processing.
  4. An item ID is appended to the output only the first time it is seen. Later occurrences, whether in the same collection or in another one, are ignored.

Validation

Before traversing, check the input. Raise a ValueError if any of the following holds:

  • root_id is not a key of collections.
  • Any collection ID listed in the "children" of any record (even one not reachable from the root) is not a key of collections.

No partial result should be returned when an error is raised.

Constraints

  • 1 <= len(collections) <= 20,000
  • Total number of items across all collections is at most 100,000.
  • Total number of child links across all collections is at most 100,000.
  • All collection IDs and item IDs are non-empty strings.
  • Every record contains both "pins" and "children" as lists.

Examples

Example 1

Input: gather_unique_items({'board_1': {'pins': ['pin_a', 'pin_b'], 'children': ['board_2', 'board_3']}, 'board_2': {'pins': ['pin_b', 'pin_c'], 'children': ['board_4']}, 'board_3': {'pins': ['pin_c', 'pin_d'], 'children': []}, 'board_4': {'pins': ['pin_e'], 'children': ['board_1']}}, 'board_1')

Output: ['pin_a', 'pin_b', 'pin_c', 'pin_d', 'pin_e']

Hints

Hint 1

Two different things need to be deduplicated: collections (to stop cycles and repeated work) and items (to emit each only once). Use a separate set for each.

Hint 2

Mark a collection as visited when you push it onto the queue, not when you pop it, and mark the root before the loop begins.

Hint 3

Because the error rule covers every record, run a full validation pass over all child references before starting the BFS rather than checking lazily during traversal.