Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
Example 1:
Input: nums = [1,2,3] Output: 6
Example 2:
Input: nums = [1,2,3,4] Output: 24
Example 3:
Input: nums = [-1,-2,-3] Output: -6
Constraints:
3 <= nums.length <= 104-1000 <= nums[i] <= 1000Problem summary: Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3]
[1,2,3,4]
[-1,-2,-3]
maximum-product-subarray)maximum-product-of-three-elements-after-one-replacement)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #628: Maximum Product of Three Numbers
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int a = nums[n - 1] * nums[n - 2] * nums[n - 3];
int b = nums[n - 1] * nums[0] * nums[1];
return Math.max(a, b);
}
}
// Accepted solution for LeetCode #628: Maximum Product of Three Numbers
func maximumProduct(nums []int) int {
sort.Ints(nums)
n := len(nums)
a := nums[n-1] * nums[n-2] * nums[n-3]
b := nums[n-1] * nums[0] * nums[1]
if a > b {
return a
}
return b
}
# Accepted solution for LeetCode #628: Maximum Product of Three Numbers
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
a = nums[-1] * nums[-2] * nums[-3]
b = nums[-1] * nums[0] * nums[1]
return max(a, b)
// Accepted solution for LeetCode #628: Maximum Product of Three Numbers
struct Solution;
impl Solution {
fn maximum_product(mut nums: Vec<i32>) -> i32 {
nums.sort_unstable();
let n = nums.len();
i32::max(
nums[0] * nums[1] * nums[n - 1],
nums[n - 1] * nums[n - 2] * nums[n - 3],
)
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3];
assert_eq!(Solution::maximum_product(nums), 6);
let nums = vec![1, 2, 3, 4];
assert_eq!(Solution::maximum_product(nums), 24);
}
// Accepted solution for LeetCode #628: Maximum Product of Three Numbers
function maximumProduct(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
const a = nums[n - 1] * nums[n - 2] * nums[n - 3];
const b = nums[n - 1] * nums[0] * nums[1];
return Math.max(a, b);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.