Shortest Run of Consecutive Shipments Meeting a Weight Quota
Last updated: November 29, 2026
Quick Overview
Given positive shipment weights in loading order and a target weight, return the length of the shortest contiguous run whose total weight is at least the target, or 0 if no run reaches it.
Amazon
November 29, 20260
0
0 solved
Given positive shipment weights in loading order and a target weight, return the length of the shortest contiguous run whose total weight is at least the target, or 0 if no run reaches it.
A common Amazon online assessment item themed on filling a trailer to its minimum billable weight. The grader looks for the variable-size sliding window that grows on the right and shrinks on the left, and for awareness that it only works because weights are positive.
What the Interviewer Expects
- Use two pointers to maintain a window sum
- Shrink from the left while the window still meets the target
- Explain why positivity makes shrinking safe
- Return 0 when the total never reaches the target
- Give O(n) time and O(1) space
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
- How would you solve it if weights could be negative (for example, returned pallets)?
- Give an O(n log n) prefix-sum plus binary search alternative.
- How would you return the earliest such run when several share the shortest length?
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: list weights of positive integers and an integer target >= 1. Output: minimum length L such that some contiguous run of length L sums to at least target, else 0....
High-Level Design
Expand the right edge one shipment at a time, adding its weight. Whenever the window sum is at least the target, record the window length and drop shipments from the left until it no longer qualifies....