← Uber Interview Insights

Uber·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber SWE coding round with two algorithm problems back to back. Nothing too crazy but the sliding window question had some edge cases I didn't love, and the DP one was pretty standard if you've seen it before.

Questions Asked (2)

Q1

Given an integer array and a limit value, find the length of the longest contiguous subarray where the difference between its max and min elements does not exceed the limit.

Algorithms & Data Structures
Author's notes

My first instinct was brute force, check every subarray and track max/min.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two pointers) to maintain a window where the difference between the maximum and minimum elements is at most the limit. Efficiently track the max and min within the window using monotonic deques, expanding the right pointer and shrinking the left pointer when the condition is violated. Keep track of the maximum window length seen.

Pro tip: Emphasize that the monotonic deque approach gives O(n) time, which is optimal, and discuss how it handles duplicates and edge cases like empty arrays or limit=0. Mention that this is a common pattern for problems involving subarray constraints.

1. Clarify and Understand the Problem

Restate the problem in your own words and confirm edge cases: empty array, single element, negative numbers, and limit=0. Ask if the array can be modified or if extra space is allowed.

2. Discuss Brute Force and Its Complexity

Mention that a brute force approach would check all subarrays, compute max and min for each, resulting in O(n^2) or O(n^3) time. This shows you understand the baseline and the need for optimization.

3. Propose the Sliding Window with Monotonic Deques

Explain that you'll maintain a window [left, right] and use two deques: one for maximums (decreasing order) and one for minimums (increasing order). Expand right, update deques, and while max-min > limit, shrink from left and update deques accordingly.

4. Walk Through an Example

Trace the algorithm on a small example, such as [8,2,4,7] with limit=4, to demonstrate how the window expands and contracts and how the deques are updated. This validates your approach.

5. Analyze Complexity and Edge Cases

State that each element is added and removed from each deque at most once, giving O(n) time and O(n) space in the worst case. Discuss edge cases like empty array, limit negative, or all elements equal.

Key Points to Mention

  • Sliding window technique with two pointers (left and right).
  • Monotonic deques to efficiently track maximum and minimum in the current window.
  • Time complexity O(n) and space complexity O(n) due to deques.
  • Handling of edge cases: empty array, single element, limit=0, negative numbers.
  • Comparison with alternative approaches like using balanced BST or segment tree, noting higher time complexity.
  • The importance of maintaining the window invariant: max - min <= limit.

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

Q2

Given a binary matrix of 0s and 1s, return the area of the largest square that contains only 1s.

Algorithms & Data Structures
Author's notes

Classic DP problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use dynamic programming to compute the size of the largest square ending at each cell, then return the square of the maximum size. Explain the recurrence: dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 if matrix[i][j] == 1, else 0. Optimize space to O(n) by keeping only the previous row.

Pro tip: After presenting the DP solution, mention that you can optimize space to O(n) by using a single array and a variable to track the previous diagonal value. This shows you think about efficiency beyond the basic solution.

1. Clarify and Confirm

Ask clarifying questions: Is the matrix guaranteed to be rectangular? Can it be empty? What are the constraints on dimensions? Confirm that the square must be axis-aligned and contain only 1s.

2. Brute Force Baseline

Mention that a brute force approach would check every possible square, which is O(m*n*min(m,n)^2) or worse. This sets the stage for optimization.

3. Dynamic Programming Insight

Explain that the largest square ending at (i,j) depends on the largest squares ending at (i-1,j), (i,j-1), and (i-1,j-1). The recurrence is dp[i][j] = min(three neighbors) + 1 if cell is 1, else 0.

4. Implement and Optimize

Write code for the DP solution, then optimize space to O(n) by using a 1D array and tracking the previous diagonal value. Analyze time complexity O(m*n) and space O(n).

5. Test and Edge Cases

Walk through a small example, test edge cases like empty matrix, all 0s, all 1s, and single row/column. Verify the recurrence and return the area (max side squared).

Key Points to Mention

  • Dynamic programming recurrence: dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 for 1s.
  • Time complexity O(m*n) and space complexity O(m*n) or O(n) with optimization.
  • Handling edge cases: empty matrix, no 1s, single row/column.
  • The area is the square of the maximum side length found.
  • Space optimization technique using a 1D array and a variable for the previous diagonal.
  • Comparison with brute force to highlight efficiency.

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