Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [1, 2, 1, 3, 2, 2, 4, 1]. What is the length of the longest contiguous subarray that contains at most two distinct values?
- A. 4
- B. 1
- C. 5
- D. 3
Correct answer: D. 3
A sliding window keeps a count of each value inside it and shrinks from the left whenever more than two distinct values appear. The window never needs to move backwards, so the scan is linear.
More Coding Interview Problems questions
- Sorted array a = [2, 3, 5, 6, 8, 10]. How many DISTINCT values appear among the sums a[i] + a[j] over pairs i < j?
- Sorted array a = [1, 2, 4, 5, 6, 9, 10, 11]. How many elements does the largest contiguous block of this array have if its largest element…
- How many permutations (p1, ..., p5) of the numbers 1 to 5 satisfy pi <= i + 1 for every position i? Use a bitmask DP over the set of…
- Choose any subset (empty allowed) of {1, 2, 3, 4, 5}. How many subsets have bitwise OR equal to 7?
- How many binary strings of length 8 contain no three consecutive 1s?
- Array [5, 2, 8, 1, 4, 6, 3]. How many index pairs i < j have a[i] AND a[j] = 0 (no common set bit)?