← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google SWE round with one algorithmic problem that looked like a grid puzzle but was really just N-Queens in disguise. Pretty classic backtracking territory once you see it.

Questions Asked (1)

Q1

Given an N x N grid where some cells contain houses ('H') and others are empty ('0'), place exactly one flower ('F') in each row and each column such that every house has at least one flower in a directly adjacent cell. Return any valid arrangement.

Algorithms & Data Structures
Author's notes

The moment I saw 'one per row, one per column' I just thought N-Queens and went straight to DFS with backtracking.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a constraint satisfaction problem where each row and column must have exactly one flower, and each house must be adjacent to at least one flower. Use backtracking with pruning to assign flowers row by row, ensuring column constraints and house coverage are satisfied. Optimize by checking feasibility early and using heuristics like placing flowers near uncovered houses.

Pro tip: Clarify that the solution is not unique and that returning any valid arrangement is acceptable; this shows you understand the problem's flexibility and can focus on correctness over optimality. Also, mention that you would test edge cases like grids with no houses or houses in corners.

1. Understand the problem and constraints

Restate the problem: place exactly one flower per row and column, and ensure every house has at least one adjacent flower. Identify that adjacency includes up, down, left, right (not diagonal).

2. Choose an algorithmic approach

Select backtracking as the primary method because it naturally handles the row-by-row placement and column uniqueness constraint. Consider alternative formulations like exact cover or SAT, but backtracking is simpler to implement.

3. Design the backtracking with pruning

For each row, try placing a flower in each column not yet used. After placement, check if any house in the current or previous rows becomes impossible to cover (e.g., a house with no adjacent empty cells left for future flowers). Prune if constraints are violated.

4. Implement and test

Write code to recursively assign flowers, backtrack when stuck, and return the first valid arrangement. Test with small grids and edge cases (e.g., all houses, no houses, houses in corners) to verify correctness.

5. Analyze complexity and discuss optimizations

Explain that worst-case time is O(N!) due to permutations, but pruning reduces practical runtime. Mention potential optimizations like ordering rows by number of houses or using bitmasks for column usage.

Key Points to Mention

  • Backtracking with pruning to efficiently search the solution space
  • Column uniqueness constraint enforced via a used-columns set or bitmask
  • House coverage check: each house must have at least one adjacent flower, considering all four directions
  • Early feasibility checks to prune branches where a house cannot be covered
  • Handling edge cases such as grids with no houses or houses in corners
  • Time complexity analysis and potential optimizations (e.g., heuristics, bitmasking)

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