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}.

  1. A. 7
  2. B. 9
  3. C. 6
  4. 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

Exams that ask this topic