Find Highest Product Segment
Company: Bytedance
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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 product `6`.
Example 2:
- Input: `[-2, 0, -1]`
- Output: `0`
- Explanation: The best segment is `[0]`.
Aim for linear time complexity.
Quick Answer: This question evaluates algorithmic problem-solving with arrays and numerical edge cases, including reasoning about products across contiguous segments and sign/zero handling.
Return the maximum product of any non-empty contiguous segment.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([2,3,-2,4],)
Expected Output: 6
Explanation: Best segment is [2,3].
Input: ([-2,0,-1],)
Expected Output: 0
Explanation: Zero is better than negative products.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.