Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Values [1, 9, 4, 12, 7, 15, 3]. How many unordered pairs of distinct positions have an absolute difference of at least 5?
- A. 11
- B. 6
- C. 13
- D. 8
Correct answer: C. 13
Of the 21 pairs, 8 differ by less than 5, so 13 differ by at least 5 (sorting plus two pointers counts them in O(n log n)). Using > 5 drops the pairs differing by exactly 5 (11), checking only adjacent positions gives 6, and 8 counts the complement.
More Coding Interview Problems questions
- How many subsets of {1, 2, ..., 12} have a sum of exactly 20?
- How many integers x with 1 <= x <= 200 have an odd number of 1-bits in their binary representation?
- Meetings (closed-open as half-open intervals) are [1,4), [1,4), [2,6), [5,8), [3,9), [7,10). A room can be reused the moment a meeting…
- Closed integer intervals [1,3], [2,5], [4,7], [6,9], [8,10], [5,6] are available. What is the minimum number needed so that every integer…
- Array [3, 1, 2, 5, 0, 4, 2]. How many contiguous subarrays have a sum equal to twice their length?
- Array [1, 3, 5, 4, 2, 6, 8, 7, 9]. What is the length of the shortest contiguous segment that, once sorted, makes the whole array sorted?