Dijkstra Shortest Paths in a Weighted Undirected Graph
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs reportedly put a weighted undirected shortest-path problem in front of candidates in September 2026, and the detail that matters is in the statement: parallel edges, self-loops, zero weights, and 64-bit distances. That's Dijkstra with a few traps stapled on. If your OA invite is sitting in your inbox, the good news is the pattern is classic graph work and the code is short. The bad news is that small slips like int overflow or a bad unreachable marker cost you hidden tests. StealthCoder is the safety net if you blank mid-assessment, but the plan below should get you most of the way.
The problem
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 = 5 edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5]] source = 0 return = [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 = 1 edges = [] source = 0 return = [0] The source has distance zero from itself. Example 3 vertexCount = 4 edges = [[0,1,10],[0,1,3],[1,2,0],[2,3,7],[0,3,20]] source = 0 return = [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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency list and add each edge in both directions. Run Dijkstra from the source with a min-heap of (distance, vertex) pairs. Initialize every distance to a large sentinel, set the source to 0, and skip any popped entry whose distance is stale. Nonnegative weights make Dijkstra correct, including zero-weight edges. Parallel edges need no special handling, because the relaxation keeps the lighter one. Self-loops never improve a distance, so they're harmless. The pitfalls are all in the details. Use long for distances, since 100000 vertices times 1e9 weight overflows 32 bits. Convert the sentinel to -1 only at the end, never mid-run, or you'll relax from a fake value. Complexity is O((V+E) log V). If you blank on the heap syntax in the live OA, StealthCoder can supply the skeleton while you check the edge cases.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Dijkstra Shortest Paths in a Weighted Undirected Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as network delay time. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Dijkstra Shortest Paths in a Weighted Undirected Graph FAQ
What's the trick in this Goldman Sachs shortest path problem?+
It's plain Dijkstra on an undirected graph. Add every edge in both directions, use a min-heap, and keep distances as 64-bit values. The statement's extras, parallel edges, self-loops and zero weights, are handled automatically by the relaxation step.
Why does the problem stress signed 64-bit arithmetic?+
Weights go up to 1e9 and paths can span up to 100000 vertices. A path sum can easily exceed 2^31. Use long in Java or C++, or Python's native integers, and your sums stay safe.
How do I handle unreachable vertices?+
Start every distance at a large sentinel like Long.MAX_VALUE. After Dijkstra finishes, replace any vertex still at the sentinel with -1. Don't relax edges from an unreached vertex, and the sentinel never gets added to anything.
Do zero-weight edges or parallel edges break Dijkstra?+
No. Dijkstra only requires nonnegative weights, and zero qualifies. Parallel edges are just extra adjacency entries, and the heap picks the cheaper one through normal relaxation. Example 3 in the problem shows both cases together.
How do I prepare for this in 48 hours?+
Write Dijkstra from scratch two or three times with a heap and a stale-entry check. Then test it on the three examples, plus a disconnected graph and a large-weight case. Aim to type it without hesitation, since the code is about 25 lines.