Data Structures & AlgorithmsHardTCS NQT · Infosys

Problem: On a line of 12 cells (1–12), a robot starts at cell 1. Each second it moves to the cell i+3 if that cell is still ≤ 12 and not yet visited; otherwise to i−1 if that is ≥ 1 and not visited; otherwise it stops. How many seconds does the robot move before it stops?

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

Correct answer: C. 6

The path is 1 → 4 → 7 → 10 → 9 → 12 → 11, after which 14 is out of range and 10 is visited, so it moves 6 times. Counting cells visited gives 7, stopping on reaching cell 12 gives 5, and trying i − 1 before i + 3 gives 11.

More Coding Interview Problems questions

Exams that ask this topic