Data Structures & AlgorithmsHardAmazon · Microsoft coding round
For the circular array [5, -3, 5, -2, 6, -8, 4], what is the maximum sum of a non-empty contiguous subarray when the subarray is allowed to wrap around the end?
- A. 11
- B. 15
- C. 13
- D. 7
Correct answer: B. 15
The best non-wrapping subarray is 5, -3, 5, -2, 6 = 11. Wrapping gives total − (minimum subarray) = 7 − (−8) = 15, the subarray [4, 5, -3, 5, -2, 6].
More Coding Interview Problems questions
- A robot moves only right or down from the top-left to the bottom-right cell of this 6×6 grid, where # is a blocked cell and . is free…
- For the array [4, 5, 0, -2, -3, 1, 7, -9, 6] and k = 5, how many non-empty contiguous subarrays have a sum divisible by k?
- How many pairs of indices (i, j) with i < j satisfy a[i] > 2·a[j] in a = [5, 2, 6, 1, 7, 3, 9, 2, 4]?
- A walker moves from (0,0) to (4,4) on lattice points using steps (1,0), (0,1) and (1,1). The point (2,2) is forbidden. How many different…
- How many permutations of {1, 2, …, 8} have exactly two fixed points (positions i with π(i) = i)?
- How many integers n with 1 ≤ n ≤ 999 have digit sum exactly 12 and contain no digit 0 (in their usual decimal form, without leading zeros)?