Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the number of distinct subarray sums of [1, 2, 3] (non-empty subarrays)? Run the code.
Problem: What is the number of distinct subarray sums of [1, 2, 3] (non-empty subarrays)? Run the code.
a = [1, 2, 3]
print(len({sum(a[i:j]) for i in range(len(a)) for j in range(i + 1, len(a) + 1)}))What is printed?- A. 5
- B. 6
- C. 4
- D. 7
Correct answer: A. 5
The subarray sums are 1, 2, 3, 3, 5, 6, so the distinct values are 1, 2, 3, 5 and 6.
More Coding Interview Problems questions
- 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…
- Problem: How many integers in [1, 100] are divisible by neither 2 nor 3? Run the code. python print(sum(1 for x in range(1, 101) if x % 2…
- Problem: How many ordered pairs (a, b) with 1 <= a, b <= 9 make a b a perfect square? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the time complexity of Johnson's all-pairs shortest path algorithm on a sparse graph with V vertices and E edges?
- Problem: Which problem asks for the minimum number of rectangles of size 1 x 2 needed to tile a board with some cells removed (domino…