There are numCourses courses, numbered 0 to numCourses − 1. Each pair [a, b] in prerequisites means course a has to be taken before course b. Prerequisites chain: if a comes before b and b comes before c, then a comes before c too. The prerequisites never form a cycle.
You are also given queries, a list of pairs [u, v]. For each one, answer whether u has to be taken before v, directly or through a chain. Return the answers as a list of booleans, in query order.
Example 1
Input: numCourses = 4, prerequisites = [[0, 1], [1, 2], [3, 2]],
queries = [[0, 2], [2, 0], [3, 1]]
Output: [true, false, false]
Why: 0 → 1 → 2 chains; nothing leads from 2 to 0 or from 3 to 12 more examples and the constraints
Example 2
Input: numCourses = 3, prerequisites = [], queries = [[0, 2], [2, 1]] Output: [false, false] Why: with no prerequisites, no course needs another
Example 3
Input: numCourses = 3, prerequisites = [[2, 0], [0, 1]],
queries = [[2, 1], [1, 2]]
Output: [true, false]
Why: 2 comes before 0, and 0 before 1, so 2 before 1Constraints
- 2 ≤ numCourses ≤ 100
- 0 ≤ number of prerequisites ≤ numCourses · (numCourses − 1) / 2, all pairs different, no cycles
- 1 ≤ number of queries ≤ 10,000
- In every pair, the two courses are different and between 0 and numCourses − 1
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Search for each queryO(q · (n + e))
- Topological order + prerequisite setsO(n · e + q)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 100 courses and 10,000 queries | Verdict |
|---|---|---|---|
| Search for each query | O(q · (n + e)) | Repeats the same searches | |
| Topological order + sets | O(n · e + q) | Best; every query is a lookup |
n = courses, e = prerequisite pairs, q = queries.
Edge cases to test
- No prerequisites. Every answer is false.
- Reversed query. If a comes before b, then [b, a] is false: the roads have a direction.
- Indirect only. With [2, 0] and [0, 1], query [2, 1] is true although no pair says so directly.
- Unconnected courses. Courses in separate parts of the graph never need each other.
- Repeated queries. The same question asked twice gets the same answer, at no extra cost after the setup.
Common mistakes
- Checking only the direct prerequisites and missing chains.
- Mixing up the direction:
[a, b]means a before b, so the search goes from u toward v. - Handing down only the course itself and not its whole
beforeset, which loses links two or more steps away.
How to talk through it
- 1Clarify. Which way does a pair point? Can there be cycles? How many queries compared with courses?
- 2Model it. Courses are nodes, prerequisites are directed edges; each query asks if v is reachable from u.
- 3Give the per-query search. One DFS per query is O(q · (n + e)); with 10,000 queries the searches repeat.
- 4Precompute. With only 100 courses, all n² answers fit in a table: fill it in topological order, then answer in O(1).
- 5Test out loud. Run a reversed query, a chain of three, and a graph with no prerequisites.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?