donnemartin/interactive-coding-challenges · error · ValueError
array must have 3 or more ints
Error message
array must have 3 or more ints
What it means
Raised by Solution.max_prod_three_nlogn when the array has fewer than 3 elements. The maximum product of three numbers is undefined without at least three values, so the method raises ValueError('array must have 3 or more ints') before sorting.
Source
Thrown at online_judges/prod_three/prod_three_solution.ipynb:157
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def max_prod_three_nlogn(self, array):\n",
" if array is None:\n",
" raise TypeError('array cannot be None')\n",
" if len(array) < 3:\n",
" raise ValueError('array must have 3 or more ints')\n",
" array.sort()\n",
" product = 1\n",
" for item in array[-3:]:\n",
" product *= item\n",
" return product\n",
"\n",
" def max_prod_three(self, array):\n",
" if array is None:\n",
" raise TypeError('array cannot be None')\n",
" if len(array) < 3:\n",
" raise ValueError('array must have 3 or more ints')\n",
" curr_max_prod_three = array[0] * array[1] * array[2]\n",
" max_prod_two = array[0] * array[1]\n",
" min_prod_two = array[0] * array[1]\n",
" max_num = max(array[0], array[1])\n",
" min_num = min(array[0], array[1])\n",
" for i in range(2, len(array)):\n",
" curr_max_prod_three = max(curr_max_prod_three,\n",View on GitHub (pinned to 358f2cc604)
Solutions
- Check len(array) >= 3 before calling and skip/handle short inputs.
- Catch ValueError at the call site and treat it as 'insufficient data'.
- Fix the data pipeline so it guarantees at least three samples.
Example fix
# before best = Solution().max_prod_three_nlogn([1, 2]) # ValueError # after nums = [1, 2] best = Solution().max_prod_three_nlogn(nums) if len(nums) >= 3 else None
Defensive patterns
Strategy: validation
Validate before calling
if len(array) >= 3:
best = Solution().max_prod_three_nlogn(array)
else:
best = None # insufficient data Type guard
def has_three_ints(x):
return isinstance(x, list) and len(x) >= 3 and all(isinstance(v, int) for v in x) Try / catch
try:
best = Solution().max_prod_three_nlogn(array)
except ValueError as e:
if '3 or more' in str(e):
best = None
else:
raise Prevention
- Length-check inputs before product-of-three routines.
- Log short datasets instead of letting guards throw in pipelines.
When it happens
Trigger: Calling max_prod_three_nlogn([1, 2]) or with an empty list.
Common situations: Small or filtered-down datasets (e.g. filtering outliers leaves 2 items); test edge cases with short arrays.
Related errors
- prices must have at least two values
- rows and cols cannot be negative
- sentence cannot be None
- rows and cols cannot be None
- num_pairs cannot be < 0
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/c5e1229acccf66fc.
Report an issue: GitHub.