Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Starting at (0,0), a robot moves on an infinite grid using the string "UURDDLLUR" (U = up, D = down, L = left, R = right). The robot visits cells; how many distinct cells does it visit including the start?

  1. A. 10
  2. B. 8
  3. C. 7
  4. D. 1

Correct answer: B. 8

The path is (0,0), (0,1), (0,2), (1,2), (1,1), (1,0), (0,0), (−1,0), (−1,1), (0,1), with 8 distinct cells. Counting every position gives 10, dropping the start gives 7, and 1 is the final distance from the start.

More Coding Interview Problems questions

Exams that ask this topic