Cheapest Model Routing Path Under a Total Latency Budget

Last updated: November 29, 2026

Quick Overview

Requests can be routed through a directed graph of gateways, caches and model replicas where each hop has a dollar cost and a latency. Given a source, a destination and a latency budget, return the minimum total cost of a path whose total latency stays within the budget, or -1.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Requests can be routed through a directed graph of gateways, caches and model replicas where each hop has a dollar cost and a latency. Given a source, a destination and a latency budget, return the minimum total cost of a path whose total latency stays within the budget, or -1.

Serving traffic for OpenAI's API involves routing across regions and replica pools where the cheapest route is not always fast enough for the customer's latency target. This resource-constrained shortest path question checks whether you see that plain Dijkstra on one metric fails, and whether you can build a label-setting search with dominance pruning and defend its correctness.

What the Interviewer Expects
  • Explain why single-metric Dijkstra gives wrong answers
  • Search over (node, latency) labels ordered by cost
  • Prune labels dominated by a cheaper label with lower or equal latency
  • Prune pushes that exceed the latency budget
  • Discuss complexity in terms of the budget and the pseudo-polynomial nature
  • Validate against brute force on small graphs
Key Topics to Cover
shortest paths
Dijkstra
constrained optimization
Pareto dominance
weighted graphs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Latencies are real-valued and large: how would you approximate?
  • Return the path itself, not just the cost.
  • Minimize latency subject to a cost cap instead: what changes?
  • Edge latencies vary with load in real time: how would the router adapt?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n nodes, directed edges (u, v, cost, latency) with non-negative integers, src, dst, max_latency.
  • Output: minimal total cost over paths with total latency at most max_latency, el...
High-Level Design

Plain Dijkstra on cost would lock in the cheap but slow route and never reconsider; on latency it would ignore cost. Instead, treat each (cost, latency) pair at a node as a label and pop labels in i...


Submit Your Answer
Markdown supported