Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A robot starts at (0, 0) facing north on an unbounded grid. Instructions: 'G' moves one step forward; 'L' turns 90° left; 'R' turns 90° right. Apply GRGLGLGGRG (10 instructions). What is the robot's distance (Manhattan) from the origin afterwards?

  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

Track the heading as an index into the four directions and turn by modular arithmetic. Turning the heading the wrong way around changes the final position.

More Coding Interview Problems questions

Exams that ask this topic