Shortest Shelf Stretch Containing Every Item on a List
Last updated: November 29, 2026
Quick Overview
A store's products are laid out along one long aisle as a list of product ids. Given a customer's list (which may require several units of the same product), return the start and end index of the shortest contiguous stretch that contains everything, or None.
Instacart
November 29, 20260
0
0 solved
A store's products are laid out along one long aisle as a list of product ids. Given a customer's list (which may require several units of the same product), return the start and end index of the shortest contiguous stretch that contains everything, or None.
This shows up in Instacart technical screens as a picking-efficiency problem. The interviewer wants a sliding window with correct counting for duplicates, and a candidate who explains the invariant rather than guessing at pointer moves.
What the Interviewer Expects
- Recognize this as minimum window with multiplicities
- Track a single 'missing' counter so each step is O(1)
- Shrink the window only while it is valid and record the best before shrinking
- Handle duplicate requirements and items not on the list
- State O(n) time with a clear argument that each pointer moves at most n times
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 the aisle is a loop, so the stretch can wrap around the end?
- What if the list can be satisfied by any of several substitute products per line item?
- How would you return all minimal stretches instead of one?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
shelf is a list of product ids in walking order. wanted is a list of ids, repeats meaning multiple units. Return (start, end) inclusive for the shortest stretch whose counts cover wanted; ties...
High-Level Design
Expand right one product at a time. Keep have counts and a scalar missing equal to the number of units still uncovered. A unit only reduces missing when have[p] is still at or below `need[p]...