Data Structures & AlgorithmsHardTCS NQT · Product Company OA
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.
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.
a = [1, 3, -2, 5] print(max(a[i] + a[j] - (j - i) for i in range(len(a)) for j in range(i + 1, len(a))))What is printed?
- A. 3
- B. 2
- C. 6
- D. 5
Correct answer: C. 6
Check every pair's score a[i] + a[j] − (j − i): (3, 5) at indices 1 and 3 gives 8 − 2 = 6, beating the adjacent pair (1, 3) and (1, 5), both 3. Charging the number of elements (j − i + 1) instead of the gap gives the tempting 5.
More Coding Interview Problems questions
- 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…
- Problem: What is the smallest integer x >= 100 divisible by 13? Run the code. python print(next(x for x in range(100, 200) if x % 13 ==…