ORIGINAL CODING QUESTION
Peak product segment
Return the largest product of a nonempty contiguous segment. Zeros and negative values are allowed.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n integer values. Print one integer as described.
Constraints
1 ≤ n ≤ 10; -3 ≤ value ≤ 3. All products fit a signed 32-bit integer.
Example 1
Input
4 2 3 -2 2
Output
6
The first two values produce 6.
Example 2
Input
3 -2 0 -1
Output
0
The zero segment produces 0.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A negative multiplier can turn a minimum product into a maximum.
Hint 2
Track both extrema ending at each position.
Hint 3
Include starting fresh at the current value.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →