Data Structures & AlgorithmsHardTCS NQT · Product Company OA
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] a[j] at most 10? Run the code.
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] * a[j] at most 10? Run the code.
a = [1, 2, 3, 4, 5] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] * a[j] <= 10))What is printed?
- A. 7
- B. 6
- C. 14
- D. 10
Correct answer: A. 7
The pairs with product ≤ 10 are (1,2), (1,3), (1,4), (1,5), (2,3), (2,4) and (2,5); (3,4) gives 12. Using < 10 drops (2,5) (6), counting ordered pairs gives 14, and starting j at i adds (1,1), (2,2), (3,3) (10).
More Coding Interview Problems questions
- Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code. python a = [3, 1, 2] mins =…
- 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] ^…