← 返回 walmartlabs 的题目列表Course Schedule II (LC 210)
类型:qbank
Both the phone screen and physical onsite coding round used LC 210 Course Schedule II. Given a course count and prerequisite pairs, return any valid order that completes every course, or an empty array when no such order exists; the onsite implementation was handwritten on a whiteboard.
Requirements
Courses are numbered from 0 through numCourses - 1.
Each prerequisite pair [a, b] means course b must be completed before course a.
Return any ordering that completes all courses while satisfying every prerequisite.
Return an empty array when completing every course is impossible.
Notes
The same LC 210 question appeared in both the phone screen and the physical onsite coding round for one loop.
The onsite required handwritten whiteboard code, so correctness had to be established without compile-and-run feedback.
For Kahn's algorithm, add the directed edge b -> a, count each course's indegree, and enqueue every zero-indegree course. Emit a course when it leaves the queue and decrement its outgoing neighbors; return the order only if exactly numCourses courses were emitted.
The algorithm runs in O(V + E) time and uses O(V) auxiliary state beyond the O(V + E) adjacency representation. A three-state DFS postorder is an equivalent alternative, with the active state detecting a cycle.
Preparation
Write a complete LC 210 implementation by hand from a blank page, including the impossible-case return and a final check that every course was emitted.
Rehearse narrating each state transition while keeping identifiers and edge directions legible enough for whiteboard review.