The Minimum Time to Complete All Deliveries problem gives you delivery locations on a number line and asks for the minimum total travel time to complete all deliveries and return to the starting point. The challenge is optimizing the route — choosing the order of deliveries to minimize total distance traveled. This Minimum Time to Complete All Deliveries coding problem uses mathematical insight about optimal traversal on a number line.
Amazon asks this to test geometric intuition and mathematical reasoning about optimal movement on a number line. The key insight — that optimal traversal oscillates between the leftmost and rightmost unvisited points — reduces to a clean formula involving intervals. The math and binary search interview pattern applies here, especially for variants requiring range queries.
Mathematical formula via sorting. Sort all delivery positions. For positions on both sides of the origin, the minimum travel time equals: 2 * sum of all intervals minus the farthest single reach in one direction. This is because you must traverse each interval at least twice (once going, once returning) except for the final leg, which you traverse only once. Sort the positions, compute interval sums, and apply the formula.
Deliveries at positions: [-3, -1, 2, 5]. Starting at 0.
Number-line traversal problems almost always have elegant closed-form solutions based on interval coverage. Sort the positions, think about how many times each segment is crossed, and look for the single "free" direction (the farthest you go in one direction without needing to backtrack). Practice problems involving minimum traversal cost on arrays of positions — they test both geometric intuition and implementation discipline, and are favorites at logistics-focused companies like Amazon.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Garden Perimeter to Collect Enough Apples | Medium | Solve | |
| Nth Digit | Medium | Solve | |
| Reach a Number | Medium | Solve | |
| Nth Magical Number | Hard | Solve | |
| Arranging Coins | Easy | Solve |