Google PhoneScreening | L3 | SDE2
Anonymous User
1518

My friend got this question today in phonescreen
You're simulating a robot moving on a width × height grid, starting at position (0, 0) and facing East. The grid is aligned with the four directions: North, East, South, West.

The robot moves based on step instructions. On each step, it tries to move 1 cell forward. If it hits a wall, it turns 90° counterclockwis and retries the same step until it moves successfully.
Implement a Robot class with the following O(1) operations:
Robot(int width, int height): Initialize the grid and robot position (0, 0)facing"East".
void step(int num): Move the robot forward num steps.
int[] getPos(): Return the current position [x, y].
String getDir(): Return the current direction as a string.

All operations must run in O(1) time.
how to do it in o(1)

Comments (5)