Problem Description
We are given a grid defined by a width and a height. A robot initially starts at (0, 0) facing East. For every instruction, the robot is asked to move a given number of steps. At every step, it first attempts to move one cell forward in the direction it is currently facing; if that move would take it out-of-bounds, the robot instead turns 90 degrees counterclockwise (to the next cardinal direction) and then retries the move. We must implement a Robot class with methods to move the robot and to return its current position and facing direction.
Key Insights
- The robot always travels along the perimeter of the grid.
- The total number of unique moves before the robot repeats its path is the cycle length: 2*(width + height - 2).
- Instead of simulating every step for a potentially huge number of moves, we can take the number of steps modulo the cycle length.
- A key edge case is when the remainder is 0 (and the robot has moved at least once): this implies the robot completed a full cycle. In this case, the position will be (0,0) but the final direction is not “East” (the initial direction) but rather the direction obtained at the end of a full perimeter walk, which is “South”.
Space and Time Complexity
Time Complexity: O(1) per query request, since each call only requires simulating up to four segments. Space Complexity: O(1) since we only store a few integer and string values.
Solution
The approach is to precompute the cycle length, which is the perimeter of the grid excluding the repeating corner (i.e. 2*(width + height - 2)). We maintain a running counter of total steps moved. When step(num) is called, we update this counter and work modulo the cycle. When retrieving the current position and direction, we simulate the robot’s move along the four segments:
- Move East for (width - 1) steps.
- Move North for (height - 1) steps.
- Move West for (width - 1) steps.
- Move South for (height - 1) steps. For the effective number of steps r (after modulo) we simulate each segment in sequence until r is exhausted. Special handling is required when r is 0 (and at least one move has been made): we set r equal to the cycle length so that we correctly compute the final direction (“South”) rather than the initial state.
Data structures used are simple integer variables and conditional checks, and the simulation directly computes the final state in constant time.
Code Solutions
Below are the code solutions in Python, JavaScript, C++, and Java. Each snippet is annotated with inline comments for clarity.