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

From [5, 9, 2, 14, 7, 3, 8], keep as many values as possible so that the largest kept value is at most twice the smallest kept value. What is the maximum number kept?

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

Correct answer: C. 4

Sort, then a window [l, r] is valid when the value at r is at most twice the value at l. Two pointers find the longest valid window; testing a window that starts at the smallest value only can miss a better one.

More Coding Interview Problems questions

Exams that ask this topic