Magicsheet logo

Sort Items by Groups Respecting Dependencies

Hard
45.9%
Updated 6/1/2025

Sort Items by Groups Respecting Dependencies

What is this problem about?

This is a high-level "Hard" problem that combines graph theory and group management. You are given nn items, each belonging to a group. Some items might not belong to any group (given group ID -1). You are also given a list of dependencies, where beforeItems[i] is a list of items that must be processed before item ii.

Your goal is to find an ordering of items such that:

  1. Items belonging to the same group appear contiguously.
  2. All item-level dependencies are respected.
  3. All group-level dependencies are respected. If no such ordering exists, return an empty list.

Why is this asked in interviews?

Companies like Citadel, Meta, and Google ask this to test a candidate's mastery of Topological Sort. This isn't just a simple sort; it's a "nested" or "hierarchical" topological sort. It evaluates whether you can abstract a complex set of rules into multiple graph problems and solve them systematically. It’s a true test of engineering design and graph algorithm implementation.

Algorithmic pattern used

The pattern is Hierarchical Topological Sort.

  1. Group Assignment: Assign unique group IDs to items with group -1.
  2. Dual Graphs: Create two directed acyclic graphs (DAGs):
    • Item Graph: Tracks dependencies between individual items.
    • Group Graph: Tracks dependencies between groups.
  3. First Sort: Perform a topological sort on the groups.
  4. Second Sort: For each group, perform a topological sort on the items within that group.
  5. Combine: Concatenate the sorted items from each group in the order determined by the group-level sort. If any topological sort fails (contains a cycle), the overall task is impossible.

Example explanation

Items 0, 1, 2. Groups: 0, 0, 1. Dependencies: 2 must come before 0.

  1. Group Graph: Group 1 (contains item 2) must come before Group 0 (contains item 0).
  2. Group Sort: [Group 1, Group 0].
  3. Item Sort in Group 1: [2].
  4. Item Sort in Group 0: [0, 1] or [1, 0]. Result: [2, 0, 1] or [2, 1, 0].

Common mistakes candidates make

The most common mistake is failing to handle group-level dependencies correctly. If item A depends on item B, and they are in different groups, then Group(A) must depend on Group(B). Another error is not assigning unique IDs to items with group -1, which makes it impossible to treat them as independent groups. Finally, candidates often struggle with the sheer amount of bookkeeping required for the in-degrees and adjacency lists of two separate graphs.

Interview preparation tip

To solve the "Sort Items by Groups Respecting Dependencies coding problem," you must be extremely comfortable with Kahn's Algorithm for topological sort. Practice implementing it quickly and correctly. When you encounter a "grouped" dependency problem, always think about how you can simplify it into two separate dependency problems. Breaking down a "Hard" problem into two "Medium" problems is a hallmark of a senior engineer.

Similar Questions