Simulate a Robot Vacuum Sweeping a Room With Turn Rules

Last updated: November 29, 2026

Quick Overview

A robot vacuum starts at a free cell of a grid (0 free, 1 wall) facing north, east, south or west. It cleans its cell, then scans left turns for an uncleaned free neighbor and moves there; if none exists it backs up one cell keeping its heading, and stops if it cannot back up. Return the number of cells cleaned.

Samsung
Coding & Algorithms
Software Engineer
Samsung
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Medium

0

0

0 solved


A robot vacuum starts at a free cell of a grid (0 free, 1 wall) facing north, east, south or west. It cleans its cell, then scans left turns for an uncleaned free neighbor and moves there; if none exists it backs up one cell keeping its heading, and stops if it cannot back up. Return the number of cells cleaned.

Samsung's coding tests lean heavily on faithful grid simulation, and a cleaning robot is a natural fit for a company that ships robot vacuums and SmartThings appliances. The grader checks that you translate an exact rule list into code without off-by-one errors in direction arithmetic or the back-up step.

What the Interviewer Expects
  • Encodes directions with parallel dr/dc arrays and modular turns
  • Implements the left-scan order exactly as specified (turn first, then test)
  • Backs up opposite to the current heading without changing heading
  • Terminates cleanly when backing up hits a wall or the edge
  • States O(R*C) time with a short justification
Key Topics to Cover
grid simulation
direction arithmetic
state machine
termination
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
  • What changes if the robot can carry only a fixed battery and must return to its dock?
  • How would you detect that the rule set causes the robot to loop forever on a different policy?
  • How would you simulate two robots sharing a room without colliding?
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 of 0/1, start (r, c) and heading d where 0=N, 1=E, 2=S, 3=W. Each loop: mark current cell cleaned; try up to four left turns, and after each turn check the cell ahead. Move into the first unclean...

High-Level Design

This is a pure state machine with state (r, c, d) plus the cleaned set. Keep dr = [-1,0,1,0], dc = [0,1,0,-1] so a left turn is (d + 3) % 4 and backing up is (r - dr[d], c - dc[d]). Treat out-...


Submit Your Answer
Markdown supported

Related Questions