Data Structures & AlgorithmsVery hardTCS Digital · Product companies
What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?
- A. 20
- B. 10
- C. 8
- D. 24
Correct answer: D. 24
Track both the running maximum and minimum product, since a negative can flip them. The best block is [-2, -4, 3] = 24; the whole array is -240, [2, -5, -2] gives only 20 and [-5, -2] only 10.
More Coding Interview Problems questions
- What is the minimum number of coins needed to make an amount of 18 using unlimited coins of denominations {1, 6, 10}?
- The array is [3, 3, 4, 4, 5, 1, 2, 2, 6, 3, 7]. What is the length of its longest non-decreasing (not strictly increasing) subsequence?
- 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…
- 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]?