Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Shops sit on a circular ring with profits p = [5, -3, 5, 4, -8, 6]. Choose a contiguous arc of at least one shop (the arc may wrap from the last shop back to the first) to maximise total profit. What is the maximum?

  1. A. 17
  2. B. 11
  3. C. 14
  4. D. 9

Correct answer: A. 17

The answer is max(best linear subarray, total - minimum subarray). The best linear run is 11, but total 9 minus the minimum subarray -8 gives 17, achieved by the wrapped arc 6, 5, -3, 5, 4.

More Coding Interview Problems questions

Exams that ask this topic