FastPrepShortest Distance in a Graph
Problems/Shortest Distance in a Graph

Shortest Distance in a Graph

Zip logoZip● MediumNEW GRADPHONE SCREEN

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report mentions an optimal-path graph problem and an unspecified harder follow-up. Graph rules and output details are omitted; this exercise uses an unweighted, undirected baseline. Approximate source match: 40%.

You are given an undirected, unweighted graph with n vertices labeled from 0 through n - 1. Each pair [u, v] in edges connects vertices u and v in both directions.

Return the minimum number of edges in a path from start to target. Return -1 if no such path exists.

  • Every edge takes one step to traverse.
  • If start equals target, return 0.
  • The graph may be disconnected or contain cycles. Duplicate edges and self-loops are allowed; they do not create a shorter route.
  • Return only the minimum distance, not the sequence of vertices.

Function

shortestDistance(n: int, edges: int[][], start: int, target: int) → int

Examples

Example 1

n = 6edges = [[0,1],[1,2],[2,5],[0,3],[3,5],[3,4]]start = 0target = 5return = 2

The path 0 -> 3 -> 5 uses two edges. No edge connects 0 directly to 5, so a one-edge path is impossible.

Example 2

n = 5edges = [[0,1],[1,2],[3,4]]start = 0target = 4return = -1

Vertices 0 and 4 belong to different connected components, so the result is -1.

Example 3

n = 3edges = [[0,1]]start = 2target = 2return = 0

The start is already the target. No edge needs to be traversed, even though vertex 2 is isolated.

Constraints

  • 1 <= n <= 20000
  • 0 <= edges.length <= 100000
  • Every edge contains exactly two vertex labels.
  • For every edge [u, v], 0 <= u, v < n.
  • 0 <= start, target < n

More Zip Phone Screen questions

  • Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
  • Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
  • Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
  • Furthest Building with Sandbags and RopesPHONE SCREEN · Seen Feb 2026
  • Phrase Covering the Most WordsPHONE SCREEN · Seen Feb 2026
  • Translate Values in a Nested MapPHONE SCREEN · Seen Feb 2026
  • Maximum Nonadjacent Sum in a General TreePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2025
  • Open the Lock and Return a Shortest PathPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2025
  • Winning Draws for a Mahjong HandPHONE SCREEN · Seen Sep 2025
  • Find Root IDs from Parent ReferencesPHONE SCREEN · Seen Aug 2025

Similar questions at other companies

  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
  • Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Shortest Distances From an Adjacency MatrixBoston Consulting Group · PHONE SCREENBoston Consulting Group interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Find a Directed Resource Access PathCoreWeave · PHONE SCREEN
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
  • Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions

Interview reports that mention this question

  • ZipHQ New Grad OA and Interview Questions: 11 Experiences

Zip interview experiences

  • Zip SWE Interview Timelines: 31 Experiences (Part 2 of 2)
  • Zip SWE OA and Interview Questions: 31 Experiences (Part 1 of 2)
All Zip interview questionsSee Zip hiring insights
Saved
class Solution {
    public int shortestDistance(int n, int[][] edges, int start, int target) {
        // Write your code here.
    }
}
Checking account…