Answer Route Queries Under a Truck Weight Limit Offline
Last updated: November 29, 2026
Quick Overview
A road network has edges each with a maximum truck weight; for each query (p, q, w), decide whether a truck of weight w can travel from p to q using only roads whose limit exceeds w. Answer all queries efficiently.
Amazon
November 29, 20260
0
0 solved
A road network has edges each with a maximum truck weight; for each query (p, q, w), decide whether a truck of weight w can travel from p to q using only roads whose limit exceeds w. Answer all queries efficiently.
Amazon Freight and middle-mile routing must respect bridge and road weight limits. The hard onsite twist is that per-query BFS is too slow; the interviewer wants offline processing that sorts roads and queries and grows a union find as the threshold drops.
What the Interviewer Expects
- Recognize per-query BFS is O(q(n + m))
- Sort roads by limit descending and queries by weight descending
- Add roads with limit greater than the current weight before answering
- Use DSU connectivity for each query
- Return answers in original query order
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if queries must be answered online, one at a time?
- How would a maximum spanning tree with binary lifting help for online queries?
- What if a road limit changes after a bridge inspection?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: n junctions, roads (u, v, limit), queries (p, q, w). A road is usable when limit > w. Output: one boolean per query in input order. n, m, q up to about 10^5....
High-Level Design
The set of usable roads shrinks as w grows, so process trucks from heaviest to lightest: as w decreases, the usable set only grows. Sort queries by w descending and roads by limit descending. Keep a p...