Minimum Score of a Path Between Cities
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Visa reported this one in July 2026, and the detail that matters is buried in the statement: cities and roads may be visited more than once. That one sentence turns a scary path problem into a simple connectivity problem. You're looking at Minimum Score of a Path Between Cities, a graph question where the answer is the smallest edge in city 1's connected component. If your OA lands in the next day or two, learn that reduction cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you shouldn't need it once you see the trick.
The problem
You are given n cities numbered from 1 through n and an array roads. Each roads[i] = [a, b, distance] describes a bidirectional road between cities a and b. The score of a path is the minimum road distance used anywhere along that path. Cities and roads may be visited more than once. Return the minimum possible score of a path from city 1 to city n. At least one such path exists. Function minScore(n: int, roads: int[][]) → int Examples Example 1 n = 4 roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]] return = 5 The path 1 -> 2 -> 4 has score min(9, 5) = 5, which is optimal. Example 2 n = 4 roads = [[1,2,2],[1,3,4],[3,4,7]] return = 2 Because revisits are allowed, a walk can use the road of length 2 before continuing to city 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Revisits are allowed, so any road reachable from city 1 can be walked to and walked back from. Since a path from 1 to n is guaranteed, every edge in the component containing city 1 (and therefore n) can be part of some valid walk. The answer is the minimum edge weight in that component. Run a BFS or DFS from city 1 over an adjacency list, and track the smallest distance across every edge you touch. Union-find works too: union all edges, then take the minimum weight among edges whose endpoint shares city 1's root. The common pitfall is running Dijkstra or hunting for the best simple path, which wastes time and gets it wrong. Another is forgetting roads are bidirectional, so you must add both directions. Complexity is O(n + m). If you freeze on the reduction during the live OA, StealthCoder can surface the component-minimum approach quickly.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum Score of a Path Between Cities 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
You've seen the question.
Make sure you actually pass Visa's OA.
Visa 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.
Minimum Score of a Path Between Cities FAQ
What's the trick in Minimum Score of a Path Between Cities?+
Because you can revisit cities and roads, any edge in city 1's connected component is usable. The answer is just the smallest edge weight in that component. You don't need shortest paths or path enumeration. One traversal from city 1 while tracking the minimum distance solves it.
Should I use BFS, DFS, or union-find?+
All three work in roughly O(n + m). BFS or DFS from city 1 over an adjacency list is the quickest to write cleanly. Union-find is nice if you prefer to skip building the graph. Pick whichever you can code without bugs under pressure. Recursion depth is the only risk with DFS.
Why doesn't Dijkstra apply here?+
Dijkstra minimizes the sum of weights along a path. This problem minimizes the smallest single edge on a walk, and revisits are allowed. So the best answer is just the lowest edge in the reachable component. Dijkstra would run fine but solves the wrong objective.
How hard is this really for a Visa OA?+
It's easy to medium. The code is short once you spot the reduction. The difficulty is mental: candidates overthink path structure. If you remember that revisits make the whole component fair game, it's a ten-minute problem. Test it on the two given examples first.
How do I prepare for this in 48 hours?+
Write a component traversal from scratch twice: build an undirected adjacency list, run an iterative BFS from node 1, and track the minimum edge weight seen. Then check edge cases like multiple edges between the same cities and nodes unreachable from 1. That covers nearly everything this question tests.