Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Lamps 1 to 20 are all off. For each pass i = 1 to 20, every lamp whose number is a multiple of i is toggled. How many lamps are on at the end?

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

Correct answer: C. 4

A lamp ends on exactly when its number has an odd count of divisors, which happens for perfect squares. Counting only the primes or the even numbers gives a different tally.

More Coding Interview Problems questions

Exams that ask this topic