Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Sorted values [1, 4, 6, 9, 13, 14, 18, 22] must be split into the fewest contiguous groups so that in every group the largest value minus the smallest is at most 5. How many groups are needed?
- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: B. 3
Greedy with two pointers: start a group at the smallest unassigned value and extend it while the spread stays within 5. Because the array is sorted, the greedy group is always as large as possible, so no split can use fewer groups.
More Coding Interview Problems questions
- Two multisets A = {7, 2, 9, 4, 5} and B = {3, 8, 1, 6, 10} are matched one-to-one, pairing each element of A with exactly one element of…
- How many permutations of (1, 2, 3, 4, 5) have every pair of adjacent entries differing by at least 2? Solve with a bitmask DP that…
- How many integers x with 0 <= x <= 1000 have exactly four 1-bits in their binary representation?
- Among the values [3, 10, 5, 25, 2, 8], what is the maximum value of x XOR y over two different elements x and y?
- From the values [5, 12, 7, 3, 9, 14, 2, 8], what is the minimum number of values you must choose so that their sum is at least 25? Meet in…
- Arrays A = [3, 7, 11, 15, 20] and B = [2, 6, 9, 13, 18] each hold distinct values. How many pairs (x in A, y in B) satisfy x + y = 22?