← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026Remote

Summary

Uber coding screen, pretty standard sliding window stuff but the k-distinct twist tripped me up for a minute.

Questions Asked (1)

Q1

Given an integer array and an integer k, find the length of the shortest contiguous subarray containing at least k distinct integers. Return -1 if none exists.

Algorithms & Data Structures
Author's notes

My first instinct was to think about longest subarray problems I'd seen before, and I almost went down the wrong path.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two pointers) to maintain a window with at most k distinct integers, expanding the right pointer and shrinking the left when the distinct count exceeds k. Track the minimum length whenever the window contains exactly k distinct integers. If no such window exists, return -1.

Pro tip: Clarify edge cases upfront (e.g., k > distinct elements, empty array) and mention that the sliding window approach is optimal for this problem because it avoids redundant checks and runs in O(n) time.

1. Understand the problem and constraints

Restate the problem: find the shortest contiguous subarray with at least k distinct integers. Discuss edge cases: k <= 0, k > total distinct elements, empty array, and duplicates.

2. Choose the right algorithm

Select sliding window (two pointers) because it efficiently finds subarrays with a constraint on distinct elements. Explain why brute force is inefficient (O(n^2) or worse).

3. Design the sliding window logic

Maintain a frequency map for elements in the window. Expand right pointer, add element to map. While distinct count > k, shrink from left. When distinct count == k, update minimum length.

4. Handle edge cases and return result

After traversal, if minimum length is still infinity, return -1. Otherwise, return the minimum length found.

5. Analyze complexity and test

State time complexity O(n) and space O(k) for the frequency map. Walk through a small example to verify correctness.

Key Points to Mention

  • Sliding window technique with two pointers (left and right).
  • Use a hash map to track frequencies of elements in the current window.
  • Condition to shrink window: when number of distinct elements exceeds k.
  • Update minimum length when distinct count equals k.
  • Time complexity O(n) and space complexity O(k).
  • Edge cases: k <= 0, k > total distinct elements, empty array.

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