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?
- A. 5
- B. 3
- C. 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
- Problem: Count integers from 1 to 100 that are divisible by 3 or by 5 but not by 15.
- Problem: Compute 2^(2^5) mod 1000 (that is, 2 raised to the power 32, taken modulo 1000).
- Problem: With coin denominations 1, 7 and 10, what is the minimum number of coins needed to make 97?
- Problem: Job sequencing: five jobs (deadline, profit) are (2, 100), (1, 19), (2, 27), (1, 25), (3, 15). Each job takes one unit of time…
- Problem: Boats carry at most 2 people and a weight limit of 10 per boat. Four people weigh 3, 5, 3 and 4. What is the minimum number of…
- Problem: What is the smallest integer x with x·x ≥ 1,000,000,000?