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?
- A. 6
- B. 8
- C. 5
- 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
- Sorted arrays [2, 6, 9, 14] and [4, 7, 11, 15] are merged into one list. How many elements of the merged list lie strictly between 5 and 12?
- Choose a from {6, 9, 12} and b from {3, 5, 10}. What is the largest possible value of a XOR b?
- Tasks AAABBC must be scheduled one per time unit; two identical tasks need at least 2 idle-free units between them (cooldown k = 2, i.e…
- Half-open intervals [1,5), [3,8), [10,12), [11,15). How many integer points lie in at least one of these intervals?
- Array [5, -5, 2, 3, -2, 7, 3]. How many contiguous subarrays have a sum that is 0, 5 or 10?
- Sorted positive array [1, 3, 4, 6, 9, 12]. How many index pairs i < j have a[i] a[j] >= 18?