Order Courses by Prerequisites, Returning the Smallest Valid Order

Read the full interview experience this question came from →

Quick Overview

Coding problem: given a number of courses and prerequisite pairs, return the lexicographically smallest order that places every course after its prerequisites, or an empty list when a cycle makes that impossible. It tests dependency-graph modeling, ordering under constraints and cycle detection.

Order Courses by Prerequisites, Returning the Smallest Valid Order

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

There are `num_courses` courses, numbered `0` to `num_courses - 1`. Some courses must be completed before others: each pair `[a, b]` in `prerequisites` means course `b` has to be finished before course `a` can be taken. Return an order for taking all of the courses in which every course appears after all of its prerequisites. When more than one order is valid, return the lexicographically smallest one: comparing two valid orders position by position, the first position at which they differ must hold the smaller course number in your answer. If no valid order exists because the prerequisites form a cycle, return an empty list. ### Function Signature ```python def order_courses(num_courses: int, prerequisites: List[List[int]]) -> List[int]: ``` ### Rules - A pair `[a, b]` means `b` comes somewhere before `a`; it says nothing about how far apart they are. - A course with no prerequisites may appear anywhere the ordering rule allows. - A non-empty answer contains every course exactly once. ### Constraints - `1 <= num_courses <= 2000` - `0 <= len(prerequisites) <= 5000` - Every pair has exactly two elements `[a, b]` with `0 <= a, b < num_courses` and `a != b`. - No pair appears twice. ### Examples **Example 1** ``` Input: num_courses = 2, prerequisites = [[1, 0]] Output: [0, 1] ``` Course 1 requires course 0, so 0 must come first. **Example 2** ``` Input: num_courses = 4, prerequisites = [[0, 3], [1, 3]] Output: [2, 3, 0, 1] ``` Courses 0 and 1 both require course 3, so a valid order starts with 2 or 3. Of all valid orders, `[2, 3, 0, 1]` is the smallest. **Example 3** ``` Input: num_courses = 3, prerequisites = [[0, 1], [1, 2], [2, 0]] Output: [] ``` Course 0 needs 1, course 1 needs 2 and course 2 needs 0, so no course can be taken first.

Overview: Coding problem: given a number of courses and prerequisite pairs, return the lexicographically smallest order that places every course after its prerequisites, or an empty list when a cycle makes that impossible. It tests dependency-graph modeling, ordering under constraints and cycle detection.

Read the full Microsoft Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Apr 2, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

There are num_courses courses, numbered 0 to num_courses - 1. Some courses must be completed before others: each pair [a, b] in prerequisites means course b has to be finished before course a can be taken.

Return an order for taking all of the courses in which every course appears after all of its prerequisites. When more than one order is valid, return the lexicographically smallest one: comparing two valid orders position by position, the first position at which they differ must hold the smaller course number in your answer. If no valid order exists because the prerequisites form a cycle, return an empty list.

Function Signature

def order_courses(num_courses: int, prerequisites: List[List[int]]) -> List[int]:

Rules

  • A pair [a, b] means b comes somewhere before a ; it says nothing about how far apart they are.
  • A course with no prerequisites may appear anywhere the ordering rule allows.
  • A non-empty answer contains every course exactly once.

Constraints

  • 1 <= num_courses <= 2000
  • 0 <= len(prerequisites) <= 5000
  • Every pair has exactly two elements [a, b] with 0 <= a, b < num_courses and a != b .
  • No pair appears twice.

Examples

Example 1

Input: num_courses = 2, prerequisites = [[1, 0]]
Output: [0, 1]

Course 1 requires course 0, so 0 must come first.

Example 2

Input: num_courses = 4, prerequisites = [[0, 3], [1, 3]]
Output: [2, 3, 0, 1]

Courses 0 and 1 both require course 3, so a valid order starts with 2 or 3. Of all valid orders, [2, 3, 0, 1] is the smallest.

Example 3

Input: num_courses = 3, prerequisites = [[0, 1], [1, 2], [2, 0]]
Output: []

Course 0 needs 1, course 1 needs 2 and course 2 needs 0, so no course can be taken first.