Data Structures & AlgorithmsVery hardProduct company OA
What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, −8, 3, 4, −2, 6, −9, 7] (a subarray may wrap from the end to the start)?
- A. 15
- B. 11
- C. 12
- D. 14
Correct answer: A. 15
Linear Kadane gives 3 + 4 − 2 + 6 = 11. The wrap-around best is total − (minimum subarray): total = 6 and the minimum subarray is just [−9], so 6 − (−9) = 15, i.e. every element except −9: 7, 5, −8, 3, 4, −2, 6. Answer max(11, 15) = 15. '12' takes only 7 + 5; '11' ignores wrapping.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the maximum product of a non-empty contiguous subarray of [2, −3, 0, −2, −4, 3, −1]?
- What is the next lexicographic permutation of [1, 5, 8, 4, 7, 6, 5, 3, 1]?
- Circular route: station i gives gas[i] and driving to station i + 1 costs cost[i], with gas = [3, 1, 5, 2, 6] and cost = [4, 3, 2, 4, 2]…
- A monotonic stack removes duplicate letters so that each letter appears exactly once and the result is the lexicographically SMALLEST…
- 20 keys are hashed independently and uniformly into 100 slots. What is the EXPECTED number of colliding pairs (unordered pairs of keys…
- How many contiguous subarrays of [1, 3, 2, 4, 6, 5] satisfy max − min ≤ 2? (Two monotonic deques shrink the window from the left.)