Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
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 minus its smallest is at most 2?
- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: B. 3
Two pointers keep the window whose spread is at most 2 and track the largest window size; because the array is sorted, the spread equals the last element minus the first. Block [4, 5, 6] is one of the windows of the maximum size.
More Coding Interview Problems questions
- 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)?
- What is the minimum number of single-bit flips needed to change the 8-bit number 13 into the 8-bit number 22?
- Consider the eight primes 2, 3, 5, 7, 11, 13, 17, 19. How many subsets (including the empty subset, whose product is 1) have a product…