Data Structures & AlgorithmsModerateGATE CS · TCS NQT

How many distinct cells are visited when a robot starts at (0, 0) and makes the moves R, U, R, D, L, U, U (R: x+1, L: x-1, U: y+1, D: y-1), counting the starting cell?

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

Correct answer: A. 6

Positions: (0,0), (1,0), (1,1), (2,1), (2,0), (1,0), (1,1), (1,2); putting them in a set leaves 6. Counting all 8 positions ignores the 2 revisits, and 3 is the Manhattan distance of the final cell from the start.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic