FastPrepDijkstra Shortest Paths in a Weighted Undirected Graph
Problems/Dijkstra Shortest Paths in a Weighted Undirected Graph

Dijkstra Shortest Paths in a Weighted Undirected Graph

Visa logoVisa● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The original report specifies Dijkstra's algorithm on a weighted undirected graph with an adjacency list and a priority queue. Our version adds a source input, output of the distance to every vertex, a marker for unreachable vertices, examples, and non-negative edge weights with limits.

You are given vertexCount vertices numbered from 0 to vertexCount - 1, an array of undirected weighted edges, and a source vertex. Each edge is [u, v, weight] with a nonnegative weight.

Return the shortest distance from source to every vertex in vertex order. Return -1 for an unreachable vertex. Parallel edges and self-loops are allowed, and all distance arithmetic must use signed 64-bit values.

Function

shortestUndirectedDistances(vertexCount: int, edges: int[][], source: int) → long[]

Examples

Example 1

vertexCount = 5edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5]]source = 0return = [0,3,1,4,-1]

Vertex 1 is reached more cheaply through vertex 2, vertex 3 then follows through vertex 1, and vertex 4 is disconnected.

Example 2

vertexCount = 1edges = []source = 0return = [0]

The source has distance zero from itself.

Example 3

vertexCount = 4edges = [[0,1,10],[0,1,3],[1,2,0],[2,3,7],[0,3,20]]source = 0return = [0,3,3,10]

The lighter parallel edge reaches vertex 1, the zero-weight edge reaches vertex 2 at the same distance, and the best route to vertex 3 costs 10.

Constraints

  • 1 <= vertexCount <= 100000.
  • 0 <= edges.length <= 200000.
  • Every edge has valid endpoints and a weight in [0, 1000000000].
  • 0 <= source < vertexCount.
  • Every reachable shortest distance fits a signed 64-bit integer.

More Visa Online Assessment questions

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Lock CodeOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
  • Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
  • Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
  • Count Valid Space-Delimited WordsOA · Seen Oct 2026
  • Earliest Common Meeting StartOA · Seen Oct 2026
  • Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
  • Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026

Similar questions at other companies

  • Minimum Stress PathWalmart · OAWalmart interview questions
  • Minimize Path ValuePalantir · OAPalantir interview questions
  • Minimize Path ValueSalesforce · OASalesforce interview questions
  • Cookie Monster Minimum Arrival TimeIntuit · OAIntuit interview questions
  • Minimum Wizard Referral CostAirbnb · OAAirbnb interview questions
  • Minimum Travel Time with Flashing Traffic LightsPeopleStrong · OAPeopleStrong interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
  • Minimum Product Path in a Directed GraphGoogle · ONSITE INTERVIEWGoogle interview questions
All Visa interview questionsSee Visa hiring insights
Saved
class Solution {
    public long[] shortestUndirectedDistances(int vertexCount, int[][] edges, int source) {
      // write your code here
    }
}
Checking account…