← Uber Interview Insights

Uber·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Apr 2026

Summary

Uber SWE online assessment, one algorithmic problem via Hack2Hire. Pretty standard sliding window stuff but the edge cases can bite you if you're not careful.

Questions Asked (1)

Q1

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

Algorithms & Data Structures
Author's notes

Sliding window with a frequency map, not too bad once you see it.

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 with at least k distinct integers, expanding the right pointer to include more distinct values and shrinking the left pointer to find the shortest valid window. Track the frequency of each integer in the window and the number of distinct integers, updating the minimum length whenever the window is valid.

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

1. Understand the problem and edge cases

Restate the problem to ensure clarity: find the shortest contiguous subarray with at least k distinct integers. Discuss edge cases like empty array, k=0, k > total distinct integers, and negative numbers.

2. Choose the sliding window approach

Explain that a brute-force solution would be O(n^2) or worse, so a sliding window (two-pointer) technique is optimal. Describe how the window expands and contracts to maintain at least k distinct integers.

3. Detail the algorithm steps

Initialize left=0, a frequency map, distinct count, and min length. Iterate right from 0 to n-1, add element to map, update distinct count. While distinct >= k, update min length, then remove left element, update map and distinct count, and increment left.

4. Analyze complexity and test

State time complexity O(n) and space O(n) due to the frequency map. Walk through a small example to verify correctness, and mention potential optimizations or alternative approaches if needed.

Key Points to Mention

  • Sliding window technique with two pointers (left and right)
  • Frequency map (hash map) to track counts of integers in the current window
  • Maintaining a count of distinct integers in the window
  • Updating the minimum length when the window is valid (distinct >= k)
  • Time complexity O(n) and space complexity O(n)
  • Edge cases: empty array, k=0, k > total distinct integers, all elements same

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