← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Uber SWE coding round, one graph problem, pretty standard stuff if you've seen this type before.

Questions Asked (1)

Q1

Given an n x n binary matrix where 1s represent land and 0s represent water, and the matrix contains exactly two islands (4-directionally connected groups of 1s), find the minimum number of 0s you need to flip to connect the two islands into one.

Algorithms & Data Structures
Author's notes

I knew the general shape of the solution pretty quickly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS to identify one island, then multi-source BFS from all cells of that island to find the shortest path to the other island. The number of steps (or layers) until reaching the second island gives the minimum number of 0s to flip.

Pro tip: Clarify that flipping 0s to 1s is equivalent to finding the shortest path through water cells; mention that you can optimize by only expanding from the smaller island to reduce BFS queue size.

1. Identify the two islands

Traverse the matrix to find the first '1' and use BFS/DFS to mark all cells of that island. The remaining unvisited '1's belong to the second island.

2. Choose the source island

Select the island with fewer cells as the source to minimize the initial BFS queue size, though either works.

3. Multi-source BFS from source island

Initialize a queue with all cells of the source island and perform BFS, expanding to neighboring water cells (0s) and unvisited land cells. Track the distance (number of water cells crossed).

4. Detect reaching the target island

When a cell belonging to the second island is reached, return the current distance (number of 0s flipped).

5. Handle edge cases and complexity

Discuss time and space complexity (O(n^2)), and consider if the islands are already connected (though problem states exactly two islands, so they are separate).

Key Points to Mention

  • BFS for shortest path in unweighted grid
  • Multi-source BFS to expand from all cells of one island simultaneously
  • Marking visited cells to avoid reprocessing
  • Distance tracking: each layer of BFS corresponds to flipping one additional 0
  • Time and space complexity: O(n^2) time and O(n^2) space
  • Optimization: choose the smaller island as the source to reduce initial queue size

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