Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

From the values [4, 9, 1, 12, 7, 3, 10, 6], keep as many as possible so that in the kept set the maximum is at most twice the minimum. How many values can be kept?

  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

After sorting, the kept set is a contiguous window whose right end is at most twice its left end. Two pointers find the widest such window in linear time, and the answer is its size.

More Coding Interview Problems questions

Exams that ask this topic