The unknown room size is what got me at first.
Model the room as an unknown grid graph and use a systematic exploration algorithm like DFS with backtracking, maintaining a visited set of relative coordinates. At each step, check isTarget(), then try moving in each direction, marking visited cells and backtracking when all options are exhausted.
Pro tip: Clearly define your coordinate system and how turns affect direction early, and separate the exploration logic from the robot API to make your solution testable and easy to reason about.
Establish a relative coordinate system (e.g., start at (0,0) facing north) and track the robot's current position and orientation. Use a set to record visited coordinates.
Select a systematic search such as DFS with backtracking, which naturally handles unknown environments and ensures complete coverage. Alternatively, BFS can be used but requires more memory for the frontier.
At each cell, check isTarget(); if true, stop. Otherwise, for each unvisited adjacent direction, move forward, recursively explore, then return to the previous cell by turning around and moving forward.
Update the robot's orientation after each turnLeft/turnRight and adjust the coordinate delta accordingly. Ensure that backtracking correctly reverses the moves made.
Discuss time complexity O(N) where N is the number of reachable cells, and space O(N) for the visited set and recursion stack. Mention handling of obstacles, dead ends, and the possibility of the target being unreachable.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.