Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
From the values [12, 3, 9, 15, 6, 20, 10, 7], choose 3 of them. What is the smallest possible difference between the largest and the smallest of the three chosen values?
- A. 5
- B. 3
- C. 2
- D. 17
Correct answer: B. 3
After sorting, the best three values form a contiguous block, so the answer is the minimum of a[i+2] - a[i] over all i. Choosing non-adjacent values can only widen the span.
More Coding Interview Problems questions
- Meetings (start, end) are (1,2), (3,4), (0,6), (5,7), (8,9), (5,9). A room hosts one meeting at a time, and a meeting may start exactly…
- Array [4, 7, 1, 9, 3, 6, 2, 5]. How many pairs of prefix indices (i < j) among the nine prefix sums (including the empty prefix) have a…
- How many integers x with 1 <= x <= 100 are divisible by 3 and have exactly two 1-bits in their binary representation?
- Positive array [2, 7, 1, 8, 3, 4, 6]. What is the largest possible sum of a contiguous subarray that does not exceed 15?
- Half-open intervals [1,5), [3,8), [6,10), [9,12) are given. How many integer points x are covered by at least two of these intervals?
- Half-open intervals [0,4), [3,7), [6,9), [8,12). How many unit cells [x, x+1) are covered by two or more intervals?