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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
sliding window
hash map
at most k distinct
two pointers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list zones (strings or ints), integer k >= 0.
  • Output: maximum length of a contiguous subarray with at most k distinct 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...


Submit Your Answer
Markdown supported