Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [3, 2, 5, 1, 4, 6, 2, 7]. Consider all contiguous subarrays and count those whose sum is a power of two from the set {1, 2, 4, 8, 16}.
- A. 7
- B. 9
- C. 6
- D. 18
Correct answer: A. 7
Build prefix sums and, for each end, test whether prefix minus one of the five powers of two appears earlier. Checking only the sums of length two overlooks longer windows, and counting even sums is a different condition.
More Coding Interview Problems questions
- Array a = [4, 7, 1, 8, 3, 6, 5, 2]. What is the largest sum of a contiguous subarray that contains exactly two odd numbers?
- Array a = [2, 5, 1, 6, 3, 8, 4]. How many ordered pairs of distinct positions (i, j) satisfy a[j] >= 2 a[i]?
- From the values [4, 9, 1, 12, 7, 3, 10, 6], keep as many as possible so that in the kept set the maximum is at most twice the minimum. How…
- Seven people weigh [3, 5, 3, 4, 2, 6, 1] (kg). A boat carries at most two people and at most 7 kg in total. What is the minimum number of…
- Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?
- Four tasks each have (time, value): (3,2), (2,5), (4,4), (5,3). Choose any subset of tasks with total time at most 9 to maximise total…