← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Uber SWE coding round, one algorithmic question on subarrays. Pretty standard session, nothing too wild, but the problem had a subtle edge case that tripped me up a bit.

Questions Asked (1)

Q1

Given an array and an integer k, find the length of the shortest subarray that contains at least k distinct integer values. Return -1 if no such subarray exists.

Algorithms & Data Structures
Author's notes

I knew sliding window was the right move pretty quickly, but fumbled a bit on when to shrink the window versus when to record the answer.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two-pointer) technique to maintain a window that contains at most k distinct integers, expanding the right pointer and shrinking the left pointer to find the shortest valid subarray. Track the frequency of each integer in the window and the number of distinct integers to efficiently update the window.

Pro tip: Clarify edge cases upfront, such as when k is greater than the total distinct integers in the array, and mention that the sliding window approach runs in O(n) time and O(k) space, which is optimal for this problem.

1. Understand the problem and edge cases

Restate the problem to ensure clarity: find the minimum length of a contiguous subarray with at least k distinct integers. Discuss edge cases like empty array, k <= 0, or k > total distinct integers.

2. Choose the sliding window approach

Explain that a brute-force solution would be O(n^2), but a sliding window can achieve O(n) by maintaining a window with a dynamic size and a frequency map.

3. Initialize data structures and pointers

Use a hash map to count frequencies of elements in the current window, and two pointers (left and right) starting at 0. Also maintain a variable for the number of distinct integers in the window.

4. Expand and shrink the window

Move the right pointer to include new elements, updating the frequency map and distinct count. When the distinct count reaches k, update the minimum length and move the left pointer to shrink the window while maintaining at least k distinct integers.

5. Return the result

After traversing the array, return the minimum length found, or -1 if no valid subarray exists.

Key Points to Mention

  • Sliding window technique with two pointers for O(n) time complexity
  • Hash map to track frequencies of elements in the current window
  • Maintaining a count of distinct integers to know when the window is valid
  • Shrinking the window from the left to find the shortest valid subarray
  • Handling edge cases such as k > total distinct integers or empty array
  • Space complexity of O(k) due to the frequency map

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.