All practice tasks

Coding (Algo)

Parallel Initialization Batches

Topological sort · Medium

Application modules initialize in dependency batches. Modules in the same batch may start together, but none may depend on another in that batch.

Return earliest possible batches sorted numerically within each batch, or null for a cycle.

Input

Call

  • Implement solve(input) and return its result. Do not print from solve.

Input object

{ n:integer, dependencies:[before:integer,after:integer][] }

Input rules

  • Duplicate dependencies are allowed.

What each field means

  • n — number of modules with IDs 0 through n - 1.
  • Each [before, after] dependency means that before must finish in an earlier batch.

Output

Return value and rules

  • Return the earliest possible batch for every module, as an array of arrays.
  • Sort module IDs numerically within each batch.
  • Return null for a dependency cycle, or [] when there are no modules.

Constraints

  • 0 <= n <= 10000; 0 <= dependencies.length <= 100000
  • Endpoints are valid; a self-edge is a cycle.

Examples

Example 1

{
  "dependencies": [
    [
      0,
      2
    ],
    [
      1,
      2
    ],
    [
      2,
      3
    ]
  ],
  "n": 4
}

Returns

[
  [
    0,
    1
  ],
  [
    2
  ],
  [
    3
  ]
]

Example 2

{
  "dependencies": [],
  "n": 0
}

Returns

[]

Required complexity

O(n log n + m) time and O(n + m) space

What this task practices

Parallel Initialization Batches is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.