← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Uber coding round for a software engineer role. One algorithmic problem, pretty involved, and they wanted a sub-quadratic solution with a full complexity breakdown.

Questions Asked (1)

Q1

You're given an integer array where each value represents a fruit type, and an integer k. For any subarray, a 'pair' exists for a fruit value if that fruit appears at least twice in the subarray (capped at one pair per fruit regardless of higher frequency). Return the count of subarrays that contain at least k such pairs. You must design a solution better than O(n²) and explain your approach with complexity analysis.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The problem reads deceptively clean but the pair-capping detail is what trips you up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with a frequency map to maintain the number of pairs in the current window. For each right endpoint, find the smallest left such that the window has at least k pairs; then all subarrays starting at or before that left and ending at right are valid, so add left to the count. This yields O(n) time and O(n) space.

Pro tip: Clarify that each fruit contributes at most one pair regardless of frequency, and mention that the sliding window works because the pair count is monotonic with window size. Also, discuss edge cases like k=0 and arrays with all distinct elements.

1. Clarify the problem and constraints

Restate the problem in your own words, emphasizing that each fruit contributes at most one pair, and confirm the input size to justify the need for an O(n) solution.

2. Design the sliding window approach

Explain how to maintain a window [left, right] and a frequency map to track the number of pairs. For each right, expand the window and update pairs; then shrink from the left while pairs >= k, counting valid subarrays.

3. Detail the counting logic

For each right, after finding the smallest left such that the window has at least k pairs, add left to the answer (since any subarray starting at index <= left and ending at right is valid).

4. Analyze complexity and edge cases

State that each element is added and removed at most once, giving O(n) time and O(n) space. Discuss edge cases: k=0 (all subarrays valid), k > maximum possible pairs (answer 0), and arrays with many duplicates.

5. Summarize and conclude

Briefly recap the approach, its efficiency, and why it meets the requirement of being better than O(n²).

Key Points to Mention

  • Sliding window technique with two pointers
  • Frequency map to track counts and pairs
  • Monotonic property: as window expands, pair count never decreases
  • Counting valid subarrays by adding left index
  • Time complexity O(n) and space complexity O(n)
  • Edge cases: k=0, k larger than possible, all distinct elements

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