Reported August 2026
Googlegraph

Directed Graph Reachability Queries

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 Google OA reported in August 2026 looks like a plain graph question, but one edge case wrecks the lazy version. Up to 100000 queries on a graph of at most 500 vertices means running a fresh search per query will bleed time. The pattern is graph reachability, and the setup is built to reward precomputation. If you've got an invite for this one, know the trick before you open it. StealthCoder sits in the background as a safety net if your mind goes blank mid-assessment, but the idea here is short enough to carry in your head.

The problem

You are given a directed graph with n vertices numbered from 0 to n - 1. Each pair [from, to] in edges adds a directed edge from from to to.
For every pair [source, target] in queries, determine whether a directed path exists from source to target.
A vertex is reachable from itself through a path of length zero. Duplicate edges do not change reachability. Return one Boolean answer per query in the same order as queries. You do not need to reconstruct any path.

Function
answerReachabilityQueries(n: int, edges: int[][], queries: int[][]) → boolean[]

Examples
Example 1
n = 4
edges = [[0,1],[1,2],[2,3]]
queries = [[0,3],[3,0],[1,1],[0,2]]
return = [true,false,true,true]
The path 0 -> 1 -> 2 -> 3 makes 3 reachable from 0. There is no path from 3 back to 0. Vertex 1 reaches itself, and 0 reaches 2.
Example 2
n = 5
edges = [[0,1],[1,2],[2,0],[2,3],[2,3]]
queries = [[3,0],[0,3],[4,4],[4,0],[2,1]]
return = [false,true,true,false,true]
Vertices 0, 1, and 2 form a directed cycle, and that cycle reaches 3. Vertex 4 is isolated but still reaches itself through a length-zero path. Repeating the edge 2 -> 3 has no effect.
Example 3
n = 6
edges = [[0,1],[0,2],[1,3],[2,3],[4,5]]
queries = [[0,3],[1,2],[4,5],[5,4],[3,3]]
return = [true,false,true,false,true]
Vertex 0 reaches 3 through either branch. The branches do not reach one another. The separate edge 4 -> 5 works only in its listed direction, and 3 reaches itself.

Constraints
1 <= n <= 500.
0 <= edges.length <= 100000.
1 <= queries.length <= 100000.
Every entry in edges and queries contains exactly two valid vertex indices.
The graph may contain cycles, self-loops, duplicate edges, and disconnected vertices.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that n is tiny and queries are huge. Precompute reachability once, then answer every query in O(1). Two clean ways. Run a BFS or DFS from each of the 500 vertices and store a boolean matrix. Or run Floyd-Warshall style transitive closure with bitsets. Dedupe nothing manually, since duplicate edges don't hurt a visited array. The pitfall is the edge case: source equals target is always true, even for an isolated vertex with no edges, so set reach[i][i] = true up front. Cycles and self-loops are harmless as long as you mark visited. Don't run a search per query, that's 100000 searches over up to 100000 edges. Build adjacency lists once, then 500 traversals. If you freeze on the closure setup during the live OA, StealthCoder is the hedge that gets you unstuck.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Directed Graph Reachability Queries 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Directed Graph Reachability Queries FAQ

What's the trick in Directed Graph Reachability Queries?+

Precompute, don't search per query. With n at most 500, run a BFS or DFS from every vertex once and store a 500 by 500 boolean table. Then each of the 100000 queries is a single lookup. Set the diagonal to true first.

Why does the naive solution fail?+

A fresh traversal per query can touch up to 100000 edges, times 100000 queries. That's far too slow. The second failure is forgetting that a vertex reaches itself, so an isolated vertex querying itself must return true.

Should I use DFS, BFS, or Floyd-Warshall?+

Any works. Repeated BFS from each node costs about n times (n plus edges), which is fine here. Floyd-Warshall is O(n^3), about 125 million simple operations at n of 500, and bitsets make it faster. BFS with adjacency lists is the safest to write quickly.

Do duplicate edges, self-loops, and cycles need special handling?+

No. A visited array makes all three harmless. Duplicates just get skipped, self-loops point to an already visited node, and cycles can't cause infinite loops. The only special case is source equals target, which is always true.

How do I prepare for this in 48 hours?+

Write BFS reachability from a source with an adjacency list until it's automatic. Then practice wrapping it in a loop over all vertices to fill a matrix. Test on a cycle, an isolated vertex, and a repeated edge. That covers everything this problem throws at you.

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