Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

Array [1, 2, 1, 3, 2, 2]. How many contiguous subarrays contain exactly two distinct values?

  1. A. 6
  2. B. 8
  3. C. 5
  4. D. 13

Correct answer: A. 6

Count subarrays with at most two distinct values minus those with at most one. Each count is a sliding window with a frequency map, and the difference isolates subarrays with exactly two.

More Coding Interview Problems questions

Exams that ask this topic