Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code.
Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code.
from itertools import combinations print(sum(1 for c in combinations(range(1, 13), 4) if sum(c) == 26))What is printed?
- A. 33
- B. 69
- C. 23
- D. 41
Correct answer: A. 33
Enumerating the 495 four-subsets of 1..12 finds 33 with sum 26. Allowing repeated numbers gives 69, using 1..11 gives 23, and using 1..13 gives 41.
More Coding Interview Problems questions
- Problem: What is the sum of the digits of the number 2^64? Run the code. python print(sum(int(c) for c in str(2 64))) What is printed?
- Problem: How many integers x in [1, 2000] satisfy x^2 ≡ x (mod 10)? Run the code. python print(sum(1 for x in range(1, 2001) if (x x - x)…
- Problem: How many inversions does the array [2, 3, 8, 6, 1] have? Run the code. python a = [2, 3, 8, 6, 1] print(sum(1 for i in…
- Problem: What is the number of distinct subarray sums of [1, 2, 3] (non-empty subarrays)? Run the code. python a = [1, 2, 3]…
- Problem: What is the sum of all two-digit numbers that are divisible by the sum of their digits? Run the code. python print(sum(x for x in…
- Problem: How many integers in [1, 1000] are both perfect squares and perfect cubes? Run the code. python print(sum(1 for x in range(1…