Shortest Stretch of a Route Covering Every Required Stop
Last updated: November 29, 2026
Quick Overview
Given the ordered sequence of stop ids a shuttle visits and a multiset of required stop ids (some required more than once), return the start and end indices of the shortest contiguous stretch that covers every requirement, or (-1, -1) if none exists.
Uber
November 29, 20260
0
0 solved
Given the ordered sequence of stop ids a shuttle visits and a multiset of required stop ids (some required more than once), return the start and end indices of the shortest contiguous stretch that covers every requirement, or (-1, -1) if none exists.
Uber onsites like problems that look like a product question (where should a batched delivery route segment start and end) but reduce to a classic sliding window with counts. The interviewer checks that you handle required multiplicities, keep a single 'missing' counter for O(1) validity checks, and shrink the window correctly.
What the Interviewer Expects
- Model requirements as a frequency map, including duplicates
- Expand the right pointer until the window is valid
- Shrink the left pointer while it stays valid and record the best
- Track validity with a single missing counter, not a full map comparison
- Return a clear sentinel when coverage is impossible
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 each stop has a dwell time and you want the shortest stretch by total time, not count?
- How would you handle the route being a cycle (loop shuttle)?
- What if requirements may be satisfied in any order but must not repeat a stop id?
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:
routelist of stop ids,requiredlist of stop ids (duplicates mean the stop must appear that many times). - Output:
(i, j)inclusive indices of the shortest window containing every requ...
High-Level Design
Keep need[stop], the count still required inside the window, and missing, the total number of required visits not yet covered. Move right forward: if need[route[right]] > 0, decrement `missing...