Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: On a line of cells 0–9, a token starts at cell 0. Each second it moves right by 2 if the cell is even, and left by 1 if the cell is odd, but never below 0 or above 9 (a blocked move leaves it in place). What is the token's position after 7 seconds?

  1. A. 9
  2. B. 8
  3. C. 14
  4. D. 10

Correct answer: B. 8

0 → 2 → 4 → 6 → 8 in four seconds; from 8 the move to 10 is blocked, so it stays at 8 for the last three seconds. Clamping to 9 instead of staying gives 9, ignoring the wall gives 14, and treating 10 as a valid cell gives 10.

More Coding Interview Problems questions

Exams that ask this topic