Data Structures & AlgorithmsHardTCS NQT · Product Company OA
Problem: How many subarrays of [2, 3, 4, 5] contain exactly one odd number? Run the code.
Problem: How many subarrays of [2, 3, 4, 5] contain exactly one odd number? Run the code.
a = [2, 3, 4, 5] print(sum(1 for i in range(4) for j in range(i + 1, 5) if sum(x % 2 for x in a[i:j]) == 1))What is printed?
- A. 8
- B. 4
- C. 6
- D. 10
Correct answer: C. 6
Odd values sit at indices 1 and 3: subarrays with only the first odd number are 4 and with only the second are 2, so 6. Counting 'at least one odd' gives 8, a bound of range(i + 1, 4) never includes the last element (4), and 10 counts all subarrays.
More Coding Interview Problems questions
- Problem: What is the length of the longest subarray of [1, 2, 3, 4, 5] whose sum is at most 10? Run the two-pointer code. python a = [1…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] a[j] at most 10? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i…
- 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…