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?
- A. 90
- B. 55
- C. 100
- 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
- Problem: How many 2-digit numbers have both digits odd? Run the code. python print(sum(1 for x in range(10, 100) if int(str(x)[0]) % 2 ==…
- Problem: How many pairs (i < j) of [1, 3, 5, 7, 9] have a[i] + a[j] >= 10? Run the code. python a = [1, 3, 5, 7, 9] print(sum(1 for i in…
- Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code. python a = [1, -2, 3, 4, -1]…
- Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code. python sq = {x x for x in range(1, 11)} cu = {x…
- Problem: What is the sum of the digits of 2^10 = 1024? Run the code. python print(sum(int(c) for c in str(2 10))) What is printed?
- Problem: What is the product of the distinct elements of [2, 2, 3, 4, 4]? Run the code. python a = [2, 2, 3, 4, 4] p = 1 for x in set(a)…