Group GitHub Actions Jobs Into Parallel Stages From Needs

Last updated: November 29, 2026

Quick Overview

A workflow file defines jobs, each with a list of jobs it needs to finish first. Return the jobs grouped into stages where every job in a stage can run in parallel once all earlier stages finish, and raise an error naming the jobs involved if the needs graph has a cycle or references a missing job.

GitHub
Coding & Algorithms
Software Engineer
GitHub
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A workflow file defines jobs, each with a list of jobs it needs to finish first. Return the jobs grouped into stages where every job in a stage can run in parallel once all earlier stages finish, and raise an error naming the jobs involved if the needs graph has a cycle or references a missing job.

GitHub Actions resolves the `needs:` keyword into an execution graph before any runner is assigned, so this maps directly onto real product logic. Interviewers look for Kahn's algorithm done level by level, clear validation errors a user could act on, and a deterministic output order.

What the Interviewer Expects
  • Models needs as edges from dependency to dependent
  • Uses Kahn's algorithm processed in BFS levels to produce stages
  • Detects cycles by counting processed jobs
  • Validates unknown job references with a useful message
  • Sorts within a stage for deterministic, testable output
  • Gives O(V + E) complexity plus sort cost
Key Topics to Cover
topological sort
Kahn's algorithm
cycle detection
DAG levels
CI pipelines
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • A job can be marked `if: failure()` so it runs only when a dependency fails. How does the scheduler change?
  • Each job has a duration. What is the minimum total wall-clock time, and which jobs are on the critical path?
  • There are only k runners available. How would you schedule stages then?
  • How would you report the actual cycle path rather than the set of stuck jobs?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: dict[job, list[needed_job]]. Output: list[list[job]] stages. A job goes into the earliest stage after all its needs. Errors: a need that names a job not in the workflow, and any cycle. With...

High-Level Design

Build in-degrees (number of needs) and a children list. Every job with zero needs forms stage 0. Removing a stage decrements the in-degree of its children; children that hit zero form the next stage. ...


Submit Your Answer
Markdown supported