Longest Driver Shift Staying Within At Most K Zones
Last updated: November 29, 2026
Quick Overview
Given the sequence of zone ids a driver passes through during a shift (one entry per minute), return the length of the longest contiguous period in which the driver touched at most k distinct zones.
Uber
November 29, 20260
0
0 solved
Given the sequence of zone ids a driver passes through during a shift (one entry per minute), return the length of the longest contiguous period in which the driver touched at most k distinct zones.
Uber uses zone-level features for dispatch and driver incentives, and a quest might reward staying near a small set of zones. In the technical screen the interviewer checks for the variable-size sliding window with a count map and that you shrink only when the invariant breaks.
What the Interviewer Expects
- Use a count map of zones in the current window
- Grow right, shrink left while distinct count exceeds k
- Delete zones whose count hits zero so len(map) is the distinct count
- Handle k = 0 and empty input
- State O(n) time with a clear argument
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
- Return the zone set and start minute of the best window
- What if each minute carries earnings and you want the max earnings window with at most k zones?
- How would you answer this for many values of k at once?
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
zones(strings or ints), integerk >= 0. - Output: maximum length of a contiguous subarray with at most
kdistinct values....
High-Level Design
Maintain a window [left, right] and a Counter of zones inside it. Add zones[right]. While the counter has more than k keys, decrement zones[left], delete it when it reaches zero, and advance...