Coding questions · ByteDance
Find Highest Product Segment
This question evaluates algorithmic problem-solving with arrays and numerical edge cases, including reasoning about products across contiguous segments and sign/zero handling.
Given an integer array nums, find the maximum product obtainable from any non-empty contiguous segment.
A contiguous segment must contain adjacent elements from the array. The array may contain positive numbers, negative numbers, and zeros.
Return the largest possible product.
Example 1:
- Input:
[2, 3, -2, 4] - Output:
6 - Explanation: The segment
[2, 3]has product6.
Example 2:
- Input:
[-2, 0, -1] - Output:
0 - Explanation: The best segment is
[0].
Aim for linear time complexity.
Maximum Product Segment
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.