← Google Interview Insights

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

Intermediate
May 2026

Summary

Google SWE interview with a graph theory problem disguised as a grid puzzle. Took me a bit to see what was actually going on under the hood.

Questions Asked (1)

Q1

You have an m x n binary grid representing a roof where 0s are leaking cells and 1s are intact. You can place planks that cover an entire row or an entire column. What is the minimum number of planks needed to cover every leaking cell?

Algorithms & Data Structures
Author's notes

I spent way too long thinking about this as a greedy coverage problem, like just pick the row or column with the most zeros and repeat.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a minimum vertex cover in a bipartite graph where left nodes are rows and right nodes are columns, and each leaking cell (0) creates an edge between its row and column. By König's theorem, the minimum vertex cover size equals the maximum matching size, which can be computed efficiently using Hopcroft-Karp or DFS-based augmenting paths. The answer is the size of the maximum matching.

Pro tip: Explicitly connect the problem to König's theorem and mention that the minimum vertex cover gives the optimal set of planks (rows and columns). This shows deep theoretical knowledge and avoids a naive greedy approach.

1. Model as a bipartite graph

Create a bipartite graph with one set of nodes for rows and another for columns. For each leaking cell (0) at (i, j), add an edge between row i and column j.

2. Identify the problem as minimum vertex cover

Covering all leaking cells with planks means selecting a set of rows and columns that cover all edges. This is exactly the minimum vertex cover problem in a bipartite graph.

3. Apply König's theorem

State that in bipartite graphs, the size of the minimum vertex cover equals the size of the maximum matching. So the answer is the maximum matching size.

4. Compute maximum matching

Use an efficient algorithm like Hopcroft-Karp (O(E√V)) or a simple DFS-based augmenting path algorithm (O(VE)). Since the graph is small (m+n nodes), either works.

5. Return the matching size

The number of edges in the maximum matching is the minimum number of planks needed. Optionally, reconstruct the actual planks from the vertex cover.

Key Points to Mention

  • Bipartite graph modeling: rows and columns as nodes, leaking cells as edges.
  • Minimum vertex cover problem and its equivalence to maximum matching via König's theorem.
  • Efficiency: Hopcroft-Karp algorithm runs in O(E√V) where E is number of leaking cells and V = m+n.
  • Edge cases: no leaking cells (answer 0), all cells leaking (answer min(m, n)).
  • Alternative greedy approaches are suboptimal; need exact algorithm.
  • Reconstruction of the actual planks from the minimum vertex cover if required.

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