Data Structures & AlgorithmsModerateTCS NQT · number puzzle
Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code.
Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code.
print(sum(1 for x in range(1, 101) if sum(map(int, str(x))) == 10))What is printed?
- A. 12
- B. 10
- C. 9
- D. 45
Correct answer: C. 9
The values are 19, 28, 37, 46, 55, 64, 73, 82 and 91; 100 has digit sum 1. Using the mod-9 shortcut x % 9 == 1 also catches 1, 10 and 100 (12), counting digit sum 9 gives 10, and testing ≥ 10 instead of == 10 gives 45.
More Coding Interview Problems questions
- Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the…
- Problem: How many pairs (i < j) of 'aabab' have s[i] = 'a' and s[j] = 'b'? Run the code. python s = 'aabab' print(sum(1 for i in…
- Problem: How many distinct products a[i] a[j] (i < j) does [1, 2, 3, 4] produce? Run the code. python from itertools import combinations…
- Problem: What is the smallest positive integer missing from [3, 4, -1, 1]? Run the code. python a = [3, 4, -1, 1] m = 1 while m in a: m +=…
- Problem: What is the sum of the lengths of all contiguous subarrays of [1, 2, 3]? Run the code. python a = [1, 2, 3] print(sum(len(a[i:j])…
- Problem: What is 6! mod 7, the value predicted by Wilson's theorem for a prime p = 7 (where (p-1)! ≡ -1 mod p, i.e. 6 mod 7)? Run the…