Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code.
Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code.
from itertools import combinations print(sum(1 for c in combinations(range(1, 11), 3) if sum(c) == 15))What is printed?
- A. 60
- B. 15
- C. 10
- D. 120
Correct answer: C. 10
Triples with sum 15 from 1..10 are (1,4,10), (1,5,9), (1,6,8), (2,3,10), (2,4,9), (2,5,8), (2,6,7), (3,4,8), (3,5,7), (4,5,6): 10. Ordered triples give 60, allowing repeats gives 15, and 120 is all 3-subsets.
More Coding Interview Problems questions
- Problem: What is the largest k such that 2^k divides 12!? Run the code. python n = 12 c = 0 p = 2 while p <= n: c += n // p p = 2 print(c)…
- Problem: What is the length of the longest common prefix of 'interview', 'internet' and 'interval'? Run the code. python import os…
- Problem: How many subsets of {1, ..., 7} contain no two numbers that differ by exactly 1? Run the code. python print(sum(1 for m in…
- Problem: What is the minimum number of moves to sort [4, 1, 3, 2] when one move takes any element to the front? Run the code (answer = n…
- Problem: What is the running time of Ford-Fulkerson when augmenting paths are found by DFS with integer capacities?
- Problem: Which algorithm finds the maximum subarray sum in O(n log n) by splitting the array and handling the crossing case?