Robot Patrolling the Border of a Warehouse Floor Grid
Last updated: November 29, 2026
Quick Overview
A security robot patrols the border of a width by height grid, starting at (0, 0) facing East. step(num) moves it num cells forward along the border, turning counterclockwise whenever the next cell is outside the grid; getPos and getDir report its state. Make step efficient even when num is up to 10^5 per call over many calls.
0
0
0 solved
A security robot patrols the border of a width by height grid, starting at (0, 0) facing East. step(num) moves it num cells forward along the border, turning counterclockwise whenever the next cell is outside the grid; getPos and getDir report its state. Make step efficient even when num is up to 10^5 per call over many calls.
A Google phone screen simulation where naive stepping works but the interviewer wants the periodic structure: the robot only ever lives on the perimeter. The key detail is the direction at the origin after a full lap, which trips up most candidates.
What the Interviewer Expects
- Notice the robot never leaves the perimeter
- Precompute perimeter cells with their arrival directions
- Reduce each step with modulo the perimeter length
- Handle the origin's direction becoming South after any movement
- Make every call O(1) after O(width + height) setup
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 obstacles can appear on the border?
- How would you support stepping backward?
- How would you coordinate two robots so they never occupy the same cell?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Grid cells (x, y) with 0 <= x < width, 0 <= y < height, width and height at least 2. Directions are East, North, West, South. The robot moves East along y = 0, North up x = width - 1, We...
High-Level Design
Build the perimeter in walking order: bottom row with East, right column with North, top row with West, left column (excluding both ends) with South. The perimeter length is `P = 2 * (width + height) ...