Clean an Unknown Room With a Robot That Senses One Cell Ahead
Last updated: November 29, 2026
Quick Overview
A cleaning robot sits in an unknown grid room with obstacles. Its API offers move() (returns False if blocked), turnLeft(), turnRight(), and clean(). Write code that cleans every reachable cell without knowing the map or the robot's coordinates.
0
0
0 solved
A cleaning robot sits in an unknown grid room with obstacles. Its API offers move() (returns False if blocked), turnLeft(), turnRight(), and clean(). Write code that cleans every reachable cell without knowing the map or the robot's coordinates.
A well-known Google onsite style problem that tests DFS with backtracking when you cannot teleport, only physically move. The interviewer checks that you track relative coordinates and orientation yourself, and that every recursive step restores the robot's position and heading.
What the Interviewer Expects
- Track position relative to the start and a heading index
- Use DFS with a visited set of relative coordinates
- Try the four directions by turning right between attempts
- Backtrack by turning around, moving, and turning back
- Explain why the robot ends where it started
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the robot's battery allows only a limited number of moves?
- Minimize total moves, not just cover the room.
- What if the room changes while cleaning (people moving)?
- Can you avoid recursion?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Only the robot API is available; the grid and start position are hidden.
- The robot starts facing up.
- Every reachable open cell must be cleaned.
- Turning and moving are the only ways to explore....
High-Level Design
Treat the start as (0, 0) facing up, with directions ordered clockwise: up, right, down, left. DFS(cell, heading): clean, mark visited, then for each of 4 turns try the cell in the current headi...