← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Uber SWE coding round, pretty standard graph/union-find territory but the follow-up on deletion is where things got interesting and honestly a bit uncomfortable.

Questions Asked (2)

Q1

You have an m x n grid of water. Land cells are added one at a time. After each addition, return the number of connected islands (4-directional adjacency).

Algorithms & Data Structures
Author's notes

Went with union-find pretty quickly, which felt right.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a dynamic connectivity problem where each added land cell may merge with adjacent islands. Use Union-Find (Disjoint Set Union) to efficiently track and update the number of islands after each addition, or alternatively BFS/DFS with careful incremental updates. Start with an empty grid and maintain a running count of islands, incrementing when a new cell is added and decrementing for each successful union with an existing island.

Pro tip: Emphasize that Union-Find with path compression and union by rank gives near O(1) amortized time per operation, making it optimal for this incremental problem. Also mention that you can avoid revisiting the entire grid by only checking the four neighbors of the newly added cell.

1. Clarify the problem and constraints

Confirm the grid dimensions, whether land cells are added at arbitrary positions, and that islands are defined by 4-directional adjacency. Discuss edge cases like duplicate additions or out-of-bounds coordinates.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently manage connected components, or BFS/DFS if you prefer a simpler but potentially less efficient approach. Explain why Union-Find is ideal for incremental connectivity.

3. Design the incremental update logic

For each added land cell, increment the island count, then check its four neighbors. For each neighbor that is land, if it belongs to a different set, union the two sets and decrement the island count.

4. Implement and optimize

Code the Union-Find with path compression and union by rank. Ensure that each addition is processed in near-constant time by only examining the four adjacent cells.

5. Analyze complexity and test

State the time complexity: O(k * α(mn)) where k is the number of additions and α is the inverse Ackermann function. Walk through a small example to verify correctness.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near O(1) operations.
  • Incremental approach: only check the four neighbors of the newly added cell, not the entire grid.
  • Maintain a running count of islands, incrementing for each new land cell and decrementing for each successful union.
  • Handle edge cases: duplicate additions, out-of-bounds coordinates, and grid boundaries.
  • Time complexity: O(k * α(mn)) where k is the number of additions, which is nearly linear.
  • Alternative approach: BFS/DFS with a visited set, but it may be less efficient for many additions.

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

Q2

Follow-up: how would you modify your solution to also support deleting land cells, not just adding them?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I kind of stalled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the original solution's data structures and whether deletions are arbitrary or only of previously added cells. Then, extend the approach by adding a removal operation that updates the same state (e.g., union-find with size tracking, or a counter with boundary checks) and handles edge cases like isolated cells or merging/splitting components.

Pro tip: Mention that deletions are often harder than additions because they can split components; if the problem allows, process operations offline in reverse to turn deletions into additions, which reuses your original solution.

1. Clarify the original solution

Restate the data structures and invariants used for adding land cells, such as union-find for connectivity or a counter for islands.

2. Identify the deletion challenge

Explain why deletion is harder: removing a cell can split a connected component, which union-find cannot handle directly.

3. Propose a modification

Describe how to adapt the solution: either use a dynamic connectivity structure, or if operations are known in advance, process them offline in reverse.

4. Handle edge cases and complexity

Discuss cases like deleting an isolated cell, deleting a cell that disconnects a component, and analyze time/space complexity of the modified approach.

Key Points to Mention

  • Union-find is not directly reversible for deletions because it cannot split components.
  • Offline reverse processing converts deletions into additions, reusing the original algorithm.
  • If online deletion is required, consider dynamic connectivity or maintaining component sizes with careful updates.
  • Edge cases: deleting a cell with no land neighbors, deleting a cell that is a bridge between components.
  • Time and space complexity trade-offs between online and offline approaches.
  • Clarify whether deletions are only of previously added cells or arbitrary cells.

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