DSA Tracker

Medium

Requirements Met (Prerequisite Tasks)

A medium Graph problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Graph
Sheets
1
Core for
11 roles
Platform
LeetCode

The problem

There are a total of numTasks tasks labeled from 0 to numTasks-1. You are given a list of prerequisite pairs and a list of queries. Each query [u, v] asks whether task u must be completed before task v based on the transitive closure of prerequisites. Return a list of boolean answers.

Example 1

Input
numTasks = 3, prerequisites = [[0,1],[1,2]], queries = [[0,2],[2,0],[1,2]]
Output
[true,false,true]
Why
0 must come before 2 (transitively), 2 cannot come before 0, and 1 must come before 2.

Example 2

Input
numTasks = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]
Output
[false,true]
Why
1 depends on 0, so 1 before 0 is false; 0 before 1 is true.

Constraints

  • 1 <= numTasks <= 100
  • 0 <= prerequisites.length <= 200

How to think about it

Updated 2026-09-09

The question asks for reachability between arbitrary pairs in a directed graph: does a directed path exist from u to v? With at most 100 tasks, computing the full transitive closure up front via Floyd-Warshall reachability or bitset propagation answers each query in O(1) time.

Approaches, worst first

  1. Per-query BFS or DFS reachability

    time O(Q * (V + E)) · space O(V + E)

    For each query `[u, v]`, launch a graph traversal starting from u to search for v. Simple, but repeats identical graph traversals across queries.

  2. Floyd-Warshall transitive closure matrixWrite this one

    time O(V^3 + Q) · space O(V^2)

    Maintain an n x n boolean matrix `isPrereq`. Set `isPrereq[u][v] = true` for each direct prerequisite `[u, v]`. Loop intermediate k from 0 to n - 1, then i and j, updating `isPrereq[i][j] = isPrereq[i][j] || (isPrereq[i][k] && isPrereq[k][j])`. Then query in O(1).

Where people lose marks · 3
  • Reversing the relationship direction: `[u, v]` prerequisite means task u must be done before task v (`u -> v`), so query `[u, v]` tests whether u can reach v.
  • Forgetting that intermediate loop k must be outermost in Floyd-Warshall transitive closure.
  • Prerequisites array can be empty, where every query must return false.

The theory behind it

Graph — the ground this problem stands on. All Graph problems

What Graph is

A graph is a network of individual points, called vertices or nodes, connected by lines called edges. Think of a subway transit map, an electrical circuit, or a web of social friends. Unlike a tree, a graph has no designated top node and no parent-child hierarchy. Connections can run one-way or both ways, and paths can loop back on themselves to form closed cycles.

When to reach for it

Reach for graph algorithms when inputs describe relationships, networks, flights between cities, course prerequisites, or clone networks. Signals include finding the shortest route across unweighted connections, ordering tasks that depend on earlier tasks, counting isolated clusters, or checking whether a path contains an infinite loop. Whenever problems present pairs of related entities and ask for reachability, distances, or dependencies, graph representations apply.

How the pattern works

First convert edge lists into an adjacency list, mapping each node to an array of its neighbors. Choose your exploration strategy based on the goal: use a queue and breadth-first search to find the shortest path in unweighted networks, or use recursion and depth-first search to explore full paths and detect cycles. Because graphs can have loops, always track visited nodes in a set or boolean array. Add nodes to the visited set at the moment they enter the queue so they are never visited twice.

What each operation costs

OperationTime
visit all nodes and edges via searchO(v + e)
topological sort using in-degree countsO(v + e)
shortest path using dijkstra with a min-heapO((v + e) log v)
What usually goes wrong with Graph
  • Adding a node to the visited set when popping from the queue instead of when pushing, which lets neighboring nodes enqueue duplicate entries and wastes memory.
  • Failing to check for cycles in directed graphs when finding prerequisite orders, causing topological sort routines to hang or return incomplete lists.
  • Assuming an input graph is fully connected and scanning from only a single starting node, missing disconnected islands and isolated components.

Which roles need this problem

Graph is a core topic for these 11 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 6 more roles, including Performance Engineer, Search Engineer, Information Retrieval Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Graph problems

Problem set and role mapping as of .