ProgrammingModerateInfosys · Cognizant

A row of positions 1 to 20 is scanned in order. A position i is picked if no position has been picked yet, or if i minus the last picked position is at least 3. What is the number of picked positions printed?

A row of positions 1 to 20 is scanned in order. A position i is picked if no position has been picked yet, or if i minus the last picked position is at least 3. What is the number of picked positions printed?
last = none; count = 0
for i = 1 to 20
    if last is none or i - last >= 3 then
        count = count + 1; last = i
    end if
end for
Print count
  1. A. 10
  2. B. 5
  3. C. 7
  4. D. 20

Correct answer: C. 7

Picks happen at 1, 4, 7, 10, 13, 16 and 19, which is 7 positions. A gap of two allows 10 picks, a strict gap of four gives 5 picks, and counting every position ignores the rule.

More Pseudocode and Code Tracing questions

Exams that ask this topic