← Google Interview Insights

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

IntermediatePrefer not to say
Apr 2026

Summary

Google onsite coding round for a SWE role. One problem the whole time, grid-based, deceptively tricky once you get into the constraint details.

Questions Asked (1)

Q1

Given an N x N grid containing houses (H) and empty cells, place flowers such that each row and column has at most one flower, and every house has exactly one adjacent flower that isn't shared with another house. Return a valid placement or indicate whether one exists.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The N-Queens comparison only hit me after I'd already gone down a wrong path.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a constraint satisfaction problem and reduce it to bipartite matching or exact cover. Explain how to construct a graph where rows and columns are nodes, and each house imposes constraints on adjacent cells, then find a matching that satisfies all constraints.

Pro tip: Clarify the adjacency definition (4-directional vs 8-directional) and whether flowers can be placed on houses; these details drastically change the solution and show attention to problem specification.

1. Clarify constraints and assumptions

Ask about adjacency (4 or 8 directions), whether flowers can be placed on houses, and if multiple valid solutions are acceptable. Confirm the grid size and input format.

2. Model as a graph problem

Represent each row and column as nodes in a bipartite graph. For each house, identify the empty cells adjacent to it and create edges representing potential flower placements that satisfy the house's requirement.

3. Reduce to bipartite matching or exact cover

Show that the problem is equivalent to finding a matching in a bipartite graph where left nodes are rows and right nodes are columns, with edges for valid flower placements. Alternatively, formulate as an exact cover problem and use Algorithm X.

4. Design an algorithm and analyze complexity

Propose an algorithm such as Hopcroft-Karp for bipartite matching or backtracking with pruning. Discuss time and space complexity, noting that the problem is NP-hard in general but may be tractable for small N or special cases.

5. Handle edge cases and validate

Consider cases with no solution, houses on edges, or multiple houses sharing adjacent cells. Describe how to verify a solution and return it, or indicate impossibility.

Key Points to Mention

  • Bipartite matching formulation with rows and columns as partitions
  • Exact cover / Algorithm X as an alternative approach
  • NP-hardness and potential need for backtracking or heuristics
  • Adjacency definition (4-directional vs 8-directional) and its impact
  • Constraint that each row and column has at most one flower
  • Handling of houses that share adjacent cells (flower cannot be shared)

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