BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / MLOps20 of 26 in topic

761. Multi-region routing: find the lowest-latency path between services

MediumMLOps Pro

Your company serves users from several cloud regions and internal services. Monitoring gives you the measured latency of every one-way network link (A to B can differ from B to A, and some links exist in one direction only). When a region's direct link is degraded, the router needs the fastest path from the entry point to the service that holds the data, even if it takes more hops.

Write lowest_latency(n, edges, src, dst).
- Nodes are numbered 0 to n - 1.
- edges is a list of [u, v, ms]: a one-way link from u to v that takes ms milliseconds, a non-negative int. There can be several links between the same pair (pick the best), and links with 0 ms (same data center).
- Return the smallest total latency from src to dst, or -1 if dst cannot be reached. From a node to itself the answer is 0.

Constraints:
- n up to 20,000 and up to 50,000 links. The number of possible routes grows exponentially with the size of the mesh, so trying every route (DFS over all paths) is far too slow. Use Dijkstra's algorithm with a heap.
- A node can be discovered first through a slow link and later through a faster one, so only treat a node as final when it comes off the heap, not when you first push it.
- 0 ms links can form a loop (A to B to A). Your code must still finish.

Functiondef lowest_latency(n, edges, src, dst)

Example 1

Input: lowest_latency(4, [[0, 1, 5], [0, 2, 1], [2, 1, 2], [1, 3, 1]], 0, 3)

Output: 4 (0 to 2 to 1 to 3)

Example 2

Input: lowest_latency(3, [[1, 0, 2]], 0, 1)

Output: -1: the only link points the other way

Constraints

  • 1 <= n <= 100,000
  • 0 <= len(edges) <= 200,000
  • Latencies are integers from 0 to 10^6
Hint 1

Build an adjacency list first.

Hint 2

Skip a popped entry if its distance is larger than dist[node].

Hint 3

Return as soon as you pop dst.

Common mistakes
  • Using BFS and counting hops instead of latency.
  • Treating one-way links as two-way.
  • Not skipping stale heap entries, which still gives the right answer but can be slow.
Understand the idea

Dijkstra's algorithm finds shortest paths in graphs with non-negative edge weights. It keeps the best known distance to each node and a heap of candidates. In the example, from 0 it settles 2 at 1 ms, then reaches 1 through 2 at 3 ms (better than the direct 5 ms), then 3 at 4 ms. The lazy deletion trick (push duplicates, skip stale ones) keeps the code short and runs in O((V + E) log V).

Topics
MLOps
Similar questions
  • Catch train-serving skew in feature pipelinesMedium
  • Drive a staged canary rollout from per-window error ratesMedium
  • Pick a champion model with guardrail metricsMedium
  • Measure feature drift with smoothed KL divergence on quantile binsMedium
All MLOps problems
Report
761 / 903
Python 3solution.pyPython idle
1
2
3
4
def lowest_latency(n, edges, src, dst):
# return the smallest total latency, or -1
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS