Pair Delivery Orders Into Vans Under a Weight Cap
Last updated: November 29, 2026
Quick Overview
Each delivery van can carry at most two orders whose combined weight does not exceed a cap. Given order weights, return the minimum number of vans needed to carry every order.
Walmart Labs
November 29, 20260
0
0 solved
Each delivery van can carry at most two orders whose combined weight does not exceed a cap. Given order weights, return the minimum number of vans needed to carry every order.
A phone screen question in the style of Walmart Labs last-mile teams that plan Walmart+ delivery runs. The interviewer checks whether you can justify the greedy pairing of heaviest with lightest and implement it with two pointers without off-by-one errors.
What the Interviewer Expects
- Sort the weights first
- Use two pointers from both ends
- Argue why pairing the heaviest with the lightest is optimal
- Handle an order heavier than the cap explicitly
- Give O(n log n) time
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 changes if a van can hold up to three orders?
- How would you also return which orders share a van?
- If weights are small integers, can you avoid the comparison sort?
- How would volume limits in addition to weight change the approach?
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: a list of positive weights and an integer cap. Each van holds one or two orders with total weight at most cap. Output: the minimum number of vans. If any single order exceeds the cap, the insta...
High-Level Design
Sort the weights. The heaviest remaining order must go in some van. If it can share with the lightest remaining order, pair them; otherwise nothing can share with it, so it ships alone. Exchange argum...