Count Robot Routes That Sweep Every Open Cell Exactly Once

Last updated: November 29, 2026

Quick Overview

A cleaning robot starts at a given cell of a grid and must reach the dock cell, moving up, down, left, or right, visiting every open cell exactly once and never entering walls. Count the distinct routes.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A cleaning robot starts at a given cell of a grid and must reach the dock cell, moving up, down, left, or right, visiting every open cell exactly once and never entering walls. Count the distinct routes.

A Google onsite grid backtracking problem with a Hamiltonian-path flavor. The interviewer checks that you count required cells upfront, mark and unmark in place, and only accept the dock when every cell has been covered.

What the Interviewer Expects
  • Counts open cells (including start and dock) before searching
  • Marks cells visited in place and restores them on backtrack
  • Accepts arrival at the dock only when the remaining count is zero
  • Explains the exponential worst case and why grids stay small
  • Mentions parity or connectivity pruning
Key Topics to Cover
Backtracking
Grid DFS
Hamiltonian path
Pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you prune using a check that the unvisited cells remain connected?
  • How would you use bitmask DP for a grid of up to 20 cells?
  • How would you return one route instead of counting?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Grid values: 1 start, 2 dock, 0 open, -1 wall. Exactly one start and one dock.
  • Output: number of 4-directional paths from start to dock that visit every non-wall cell exactly once.
  • Grid size: at...
High-Level Design
  1. Scan the grid: record the start, and count need = number of non-wall cells.
  2. DFS from the start with remaining = need. Entering a cell decrements remaining. Mark the cell as -1 (visited), rec...

Submit Your Answer
Markdown supported