Reported September 2026
Googleheap priority queue

Find a Valid Course Completion Order

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The whole problem hinges on a min-heap. Google reported this OA in September 2026, and it looks like Course Schedule II with one twist: you must return the lexicographically smallest valid order, not just any order. That twist is what trips people up. A plain queue gives a valid topological sort, but not the smallest one. If you have the invite and 48 hours, learn this one pattern cold. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the solution live.

The problem

There are numCourses courses labeled from 0 to numCourses - 1. Each pair [course, prerequisite] means that prerequisite must be completed before course.
Return the lexicographically smallest order that completes every course. At each step, choose the smallest numbered course whose prerequisites have all been completed. If a directed cycle makes it impossible to complete every course, return an empty array.

Function
findCourseOrder(numCourses: int, prerequisites: int[][]) → int[]

Examples
Example 1
numCourses = 4
prerequisites = [[1,0],[2,0],[3,1],[3,2]]
return = [0,1,2,3]
Course 0 unlocks courses 1 and 2. Choosing 1 first makes the complete order lexicographically smallest.
Example 2
numCourses = 5
prerequisites = [[2,0],[2,1],[3,1]]
return = [0,1,2,3,4]
Courses 0, 1, and 4 initially have zero indegree. The smallest available course is always chosen.
Example 3
numCourses = 2
prerequisites = [[1,0],[0,1]]
return = []
The two courses form a directed cycle, so no complete order exists.

Constraints
1 <= numCourses <= 2000.
0 <= prerequisites.length <= 5000.
prerequisites[i].length == 2.
0 <= course, prerequisite < numCourses.
Every prerequisite pair is unique, and a course is never its own prerequisite.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is Kahn's algorithm with a priority queue instead of a FIFO queue. Build an adjacency list and an indegree array from the pairs, where [course, prerequisite] means an edge from prerequisite to course. Push every course with indegree zero into a min-heap. Pop the smallest, append it to the result, and decrement the indegree of its neighbors. Push any neighbor that hits zero. If the result length ends up below numCourses, there's a cycle, so return an empty array. The common pitfall is reversing the edge direction, or using a plain queue and failing example 2's ordering. Course 4 has no edges at all, so it must still enter the heap at the start. Complexity is O((V+E) log V), easily fine for 2000 courses and 5000 pairs. StealthCoder is your hedge if the heap detail slips under pressure during the live OA.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Find a Valid Course Completion Order cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as course schedule ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find a Valid Course Completion Order FAQ

What's the trick to this Google OA problem?+

Use Kahn's topological sort, but swap the queue for a min-heap. Every time you pop, you take the smallest course whose prerequisites are all done. That greedy choice at each step guarantees the lexicographically smallest complete order.

How is this different from LeetCode Course Schedule II?+

Course Schedule II accepts any valid order. This version demands the lexicographically smallest one, so a regular queue fails. The input format and cycle handling are the same, but the heap is the required change.

How do I detect the cycle case?+

Count how many courses you output. If the count is less than numCourses when the heap empties, some courses never reached indegree zero because they sit on a cycle. Return an empty array in that case.

Do isolated courses with no prerequisites matter?+

Yes. A course like 4 in example 2 has indegree zero from the start, so it goes into the heap right away. It gets popped in its sorted position, not at the end unless its number is largest.

How should I prepare in 48 hours?+

Write Kahn's algorithm from memory twice, once with a queue and once with a heap. Then test it on the three examples, including the cycle. Know the edge direction cold, since reversing it is the most common bug.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it