Find the minimum number of hops between node pairs in a directed graph

Quick Overview

Given a directed graph as an edge list and a set of node pairs, return the fewest edges needed to get from each start node to its target, or -1 when no path exists. It tests shortest paths in unweighted graphs, adjacency lists, and handling of cycles, unreachable nodes and self-queries.

Find the minimum number of hops between node pairs in a directed graph

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of directed edges `edges` where `edges[i] = [u, v]` indicates a directed edge from node `u` to node `v`, and a list of `queries` where `queries[i] = [A, B]`. For each query, return the minimum number of hops (edges traversed) required to reach node `B` from node `A` in the directed graph. If node `B` is not reachable from node `A`, return `-1` for that query. Return a list of integers with the answer for each query, in query order. ### Function Signature ```python def hops_to_reach_destination(edges: list[list[int]], queries: list[list[int]]) -> list[int]: ``` ### Rules - An edge `[u, v]` can only be followed from `u` to `v`. - When `A == B`, the answer is `0`. - A query may name a node that appears in no edge. Such a node reaches only itself. ### Constraints - `1 <= len(edges) <= 10^4` - `len(edges[i]) == 2` - `0 <= edges[i][0], edges[i][1] <= 10^4` - `1 <= len(queries) <= 100` - `len(queries[i]) == 2` - `0 <= queries[i][0], queries[i][1] <= 10^4` ### Examples ```text Input: edges = [[0,1],[1,2],[2,3],[0,3]], queries = [[0,3],[3,0],[0,2]] Output: [1,-1,2] ``` The direct edge 0->3 gives 1 hop. Node 3 has no outgoing edges, so 0 is unreachable from it, and 0->1->2 takes 2 hops. ```text Input: edges = [[1,2],[2,3],[3,4],[4,5]], queries = [[1,5],[1,1]] Output: [4,0] ``` The path 1->2->3->4->5 takes 4 hops. In the second query the start equals the destination, so the answer is 0 hops. ```text Input: edges = [[0,1],[1,0],[1,2],[3,0]], queries = [[0,2],[0,3],[1,0]] Output: [2,-1,1] ``` The path 0->1->2 takes 2 hops, and the edge 1->0 reaches 0 in 1 hop. From 0 only nodes 0, 1 and 2 can be reached (the edge 3->0 points into 0), so the second answer is -1, and the search must stop even though 0 and 1 form a cycle.

Overview: Given a directed graph as an edge list and a set of node pairs, return the fewest edges needed to get from each start node to its target, or -1 when no path exists. It tests shortest paths in unweighted graphs, adjacency lists, and handling of cycles, unreachable nodes and self-queries.

Salesforce logo
Salesforce
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of directed edges edges where edges[i] = [u, v] indicates a directed edge from node u to node v, and a list of queries where queries[i] = [A, B].

For each query, return the minimum number of hops (edges traversed) required to reach node B from node A in the directed graph. If node B is not reachable from node A, return -1 for that query.

Return a list of integers with the answer for each query, in query order.

Function Signature

def hops_to_reach_destination(edges: list[list[int]], queries: list[list[int]]) -> list[int]:

Rules

  • An edge [u, v] can only be followed from u to v .
  • When A == B , the answer is 0 .
  • A query may name a node that appears in no edge. Such a node reaches only itself.

Constraints

  • 1 <= len(edges) <= 10^4
  • len(edges[i]) == 2
  • 0 <= edges[i][0], edges[i][1] <= 10^4
  • 1 <= len(queries) <= 100
  • len(queries[i]) == 2
  • 0 <= queries[i][0], queries[i][1] <= 10^4

Examples

Input: edges = [[0,1],[1,2],[2,3],[0,3]], queries = [[0,3],[3,0],[0,2]]
Output: [1,-1,2]

The direct edge 0->3 gives 1 hop. Node 3 has no outgoing edges, so 0 is unreachable from it, and 0->1->2 takes 2 hops.

Input: edges = [[1,2],[2,3],[3,4],[4,5]], queries = [[1,5],[1,1]]
Output: [4,0]

The path 1->2->3->4->5 takes 4 hops. In the second query the start equals the destination, so the answer is 0 hops.

Input: edges = [[0,1],[1,0],[1,2],[3,0]], queries = [[0,2],[0,3],[1,0]]
Output: [2,-1,1]

The path 0->1->2 takes 2 hops, and the edge 1->0 reaches 0 in 1 hop. From 0 only nodes 0, 1 and 2 can be reached (the edge 3->0 points into 0), so the second answer is -1, and the search must stop even though 0 and 1 form a cycle.