FastPrepShortest Path Through a Required Waypoint
Problems/Shortest Path Through a Required Waypoint

Shortest Path Through a Required Waypoint

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Google MLE onsite report asks whether a start can reach a target in a weighted graph, for the shortest distance, and then for a route required to pass through a third vertex; the candidate used Dijkstra. The report omits direction, weight and return conventions. This practice uses nonnegative directed edges and returns direct and waypoint distances, with -1 for unreachable. Approximate source match: 82%.

Given a directed weighted graph, find the shortest distance from start to target, and the shortest distance for a walk that visits waypoint on its way from start to target.

Vertices are labeled 0 through n-1. Each edge [from,to,weight] can be traversed in its given direction and has nonnegative weight. Multiple edges and self-loops are allowed. A walk may repeat vertices or edges, and waypoint may equal an endpoint.

Return [direct, via]. Set a result to -1 when that route does not exist. A zero-length walk from a vertex to itself has distance 0.

Function

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

Examples

Example 1

n = 4edges = [[0,1,2],[1,3,4],[0,2,1],[2,3,10],[1,2,1]]start = 0target = 3waypoint = 2return = [6,11]

The shortest direct route has cost 6. Passing through vertex 2 costs 11 via the direct edge to 2.

Example 2

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

Vertex 2 is unreachable, even after passing through 1.

Example 3

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

When the waypoint is the start, the two shortest distances are equal.

Constraints

  • 1 <= n <= 1000.
  • 0 <= edges.length <= 5000.
  • Each edge is [from,to,weight] with valid vertices and 0 <= weight <= 10000.
  • start, target, and waypoint are valid 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

  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Path With Maximum ProbabilitySalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Open the Lock and Return a Shortest PathZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
  • Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Single-Source Shortest PathsRippling · ONSITE INTERVIEWRippling interview questions

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
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] shortestDistancesWithWaypoint(int n, int[][] edges, int start, int target, int waypoint) {
        // Write your code here.
    }
}
Checking account…