Google PhoneScreening | L3 | SDE2

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-03-31Added By Anonymous
Source: Link
🧭 Experience: unknown ✅ Outcome: unknown 💰 Compensation: unknown 📍 Location: unknown 🏢 Company: Google 🎓 Education: unknown 🎁 Benefits: unknown 📅 Offer Date: unknown

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