Data Structures & AlgorithmsModerateTCS NQT · basics

Problem: What is the sum of the first 10 odd positive integers? Run the code.

Problem: What is the sum of the first 10 odd positive integers? Run the code.
print(sum(2 * i + 1 for i in range(10)))
What is printed?
  1. A. 90
  2. B. 55
  3. C. 100
  4. D. 110

Correct answer: C. 100

2i + 1 for i = 0..9 lists the first ten odd numbers 1, 3, …, 19, and the sum of the first n odd numbers is n² = 100. Summing 1..10 gives 55, the even numbers 2..20 give 110, and the even numbers 0..18 give 90.

More Coding Interview Problems questions

Exams that ask this topic