FastPrepDirected Graph Reachability Queries
Problems/Directed Graph Reachability Queries

Directed Graph Reachability Queries

Google logoGoogle● HardFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was a PhD intern interview. A 2026 Google internship report described building a relationship graph with a dictionary and using BFS for reachability, followed by several extensions that were not specified. An earlier report kept the same graph-reachability task and a clue about two-dimensional dynamic programming. It left out the direction of edges, the shape of queries, the edge rules, and the input limits. The task stays unchanged from before.

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 = 4edges = [[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 = 5edges = [[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 = 6edges = [[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.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Parallel Courses IIISnowflake · ONSITE INTERVIEWSnowflake interview questions
  • Select Transactions with Child-Pays-for-Parent PackagesCoinbase · PHONE SCREENCoinbase interview questions
  • Grid Robot Route with Charging PrioritiesUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
  • Choose a Road to Maximize Average Route Toll IncreaseHarness · PHONE SCREEN
  • Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Process Graph Rule ValidationHadrian · PHONE SCREENHadrian interview questions
  • Minimum Semesters With Course CapacityAmazon · ONSITE INTERVIEW
  • Longest Decreasing Matrix Path with Limited RelaxationsDuolingo · ONSITE INTERVIEWDuolingo interview questions
  • Shortest Path Visiting All NodesInMobi · ONSITE INTERVIEWInMobi interview questions
  • Best Currency Conversion PathSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions

Interview reports that mention this question

  • Google New Grad Hard Virtual Onsite: 1 Experience
  • Google SWE and PhD Intern Coding: 14 Experiences (Part 3 of 3)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean[] answerReachabilityQueries(int n, int[][] edges, int[][] queries) {
        // Write your code here.
    }
}
Checking account…