← Uber Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Uber onsite coding round for a SWE role, one problem the whole time with a side task of writing your own test cases and a tree-print helper. The main problem was a classic dynamic union-find setup and the round felt pretty focused, no behavioral fluff.

Questions Asked (1)

Q1

You start with an m x n water grid. Land cells are added one at a time via a list of positions. After each addition, return the current number of 4-directionally connected islands.

Algorithms & Data Structures
Author's notes

I knew this was a union-find problem pretty fast, which was a relief.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use Union-Find (Disjoint Set Union) to dynamically track connected components as land cells are added. For each new land cell, increment the island count by 1, then check its four neighbors; if a neighbor is land, union the two cells and decrement the count for each successful union. This yields O(α(mn)) time per addition, which is optimal.

Pro tip: Mention that you can optimize by only checking neighbors that have already been added (e.g., up and left) to avoid redundant checks, and emphasize that Union-Find with path compression and union by rank is the standard efficient solution for dynamic connectivity problems like this.

1. Clarify the problem and constraints

Confirm the grid dimensions, the order of additions, and that islands are 4-directionally connected. Ask about edge cases like duplicate positions or out-of-bounds coordinates.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently manage connected components. Explain that it supports near-constant time union and find operations with path compression and union by rank.

3. Design the algorithm

Initialize a parent array for all cells, a rank array, and a counter for islands. For each position, if already land, skip; else mark as land, increment island count, and for each valid neighbor that is land, union the cells and decrement count on successful union.

4. Analyze complexity and edge cases

State that each addition takes O(α(mn)) amortized time, where α is the inverse Ackermann function, and space is O(mn). Discuss handling duplicate positions, boundary checks, and the initial state (zero islands).

5. Test with examples

Walk through a small example (e.g., 3x3 grid with positions [[0,0],[0,1],[1,2],[2,1]]) to verify the island count updates correctly after each addition.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near-constant time operations.
  • Increment island count for each new land cell, then decrement for each successful union with an existing land neighbor.
  • Only check neighbors that are within bounds and already land (e.g., up, down, left, right).
  • Time complexity: O(k α(mn)) where k is number of positions, space O(mn).
  • Handle duplicate positions by checking if the cell is already land and skipping if so.
  • Alternative approaches like BFS/DFS per addition are less efficient (O(k * mn)) and should be mentioned as suboptimal.

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