Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Sorted arrays A = [1, 5, 9, 12] and B = [3, 4, 10, 15, 20]. How many pairs (x in A, y in B) satisfy |x - y| <= 2?
- A. 6
- B. 4
- C. 5
- D. 2
Correct answer: C. 5
With both arrays sorted, a pointer into B that advances past values too small for x gives each x its window of valid y in linear total time. A strict bound drops pairs that differ by exactly 2.
More Coding Interview Problems questions
- 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?
- 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?