Course Schedule
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in March 2020 hands you numCourses and a list of [course, prerequisite] pairs, then asks one thing: can every course be finished, or is there a cycle. That's cycle detection in a directed graph, dressed up as a schedule. Example 2 shows it plainly: course 0 needs 1, course 1 needs 0, nobody ever starts. The report also says the interviewer pushed on alternative solutions, edge cases, and complexity. If you blank on the approach, StealthCoder runs invisibly on your screen during the live OA and gives you a working solution as a safety net.
The problem
There are numCourses courses labeled from 0 to numCourses - 1. Each pair [course, prerequisite] means the prerequisite must be completed before the course. Return true if all courses can be completed, or false if the prerequisite graph contains a cycle. Interview Follow-up The interviewer asked follow-up questions about alternative solutions, edge cases, and complexity analysis. Function canFinish(numCourses: int, prerequisites: int[][]) → boolean Examples Example 1 numCourses = 2 prerequisites = [[1,0]] return = true Example 2 numCourses = 2 prerequisites = [[1,0],[0,1]] return = false Each course requires the other first, creating a cycle.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency list from the pairs, then pick one of two standard approaches. Kahn's algorithm: compute in-degrees, push every zero in-degree course into a queue, pop and decrement neighbors, and count how many courses you processed. If the count equals numCourses, return true. The alternative is DFS with three states: unvisited, visiting, done. Reaching a visiting node means a cycle. Both run in O(V + E) time and O(V + E) space. The common pitfall is forgetting disconnected components, so start DFS from every node, not just node 0. Another is reversing the edge direction, which is harmless for cycle detection but wrecks you if the follow-up asks for an ordering. Also handle empty prerequisites and self-loops like [0,0]. Know both solutions cold, since the follow-up asks for alternatives. If your mind goes blank mid-assessment, StealthCoder is the hedge that reads the prompt and hands you working code.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Course Schedule 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as course schedule. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg 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.
Course Schedule FAQ
What's the trick to Course Schedule?+
It's cycle detection in a directed graph. Treat each prerequisite pair as an edge, then check if the graph has a cycle. No cycle means all courses can be completed. Topological sort via Kahn's algorithm or a DFS with visiting and done states both work cleanly.
How hard is this Bloomberg question really?+
Medium. The logic is short once you recognize the graph. Most people lose points on bookkeeping: edge direction, disconnected components, and detecting a cycle without false positives in DFS. If you've written a topological sort once, this takes about ten minutes.
BFS or DFS, which should I pick?+
Kahn's BFS is easier to get right under pressure because there's no recursion state to track. You count processed nodes and compare to numCourses. DFS is shorter but needs three states. Since the interviewer asked about alternatives, be able to explain both.
What edge cases should I test?+
Empty prerequisites list should return true. A self-loop like [0,0] should return false. Disconnected components need handling, so loop over every course. Also test duplicate pairs, and a long chain with no cycle to make sure recursion depth isn't an issue.
What complexity should I state?+
Time is O(V + E), where V is numCourses and E is the number of prerequisite pairs, since each node and edge is visited once. Space is O(V + E) for the adjacency list plus the in-degree array or visited states. DFS adds recursion stack depth up to O(V).