Data Structures & AlgorithmsModerateTCS NQT · Product Company OA
Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code.
Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code.
a = [3, 1, 2]
mins = {min(a[i:j]) for i in range(len(a)) for j in range(i + 1, len(a) + 1)}
print(len(mins))What is printed?- A. 2
- B. 3
- C. 4
- D. 6
Correct answer: B. 3
The minima are 3, 1 and 2, so there are 3 distinct values even though 6 subarrays exist.
More Coding Interview Problems questions
- Problem: What is the maximum value of a[i] + a[j] - (j - i) over pairs i < j of a = [1, 3, -2, 5]? Run the code. python a = [1, 3, -2, 5]…
- Problem: What is the total number of 1-bits in the binary representations of 1 through 10? Run the code. python…
- Problem: What is the minimum number of coins from {1, 2, 5} that sum to 11? Run the DP. python INF = 10 9 d = [0] + [INF] 11 for v in…
- Problem: How many binary strings of length 3 contain an even number of 1s? Run the code. python print(sum(1 for t in range(8) if…
- Problem: What is the sum of a[i] XOR a[j] over all pairs i < j of [1, 2, 3, 4]? Run the code. python a = [1, 2, 3, 4] print(sum(a[i] ^…
- Problem: How many distinct paths lead from vertex 0 to vertex 3 in the DAG with edges 0->1, 0->2, 1->2, 1->3, 2->3? Run the DP over the…