donnemartin/interactive-coding-challenges · error · TypeError
array cannot be None
Error message
array cannot be None
What it means
Raised by Solution.max_prod_three_nlogn when array is None. The O(n log n) approach sorts the array in place and multiplies the last three elements, so a None input is rejected with TypeError before sorting would fail.
Source
Thrown at online_judges/prod_three/prod_three_solution.ipynb:155
},
{
"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",View on GitHub (pinned to 358f2cc604)
Solutions
- Coalesce None to [] (then the length guard gives a clear ValueError) or filter the data source.
- Check array is not None at the call site.
- Pass sorted(array) or a copy if mutation of the original matters.
Example fix
# before Solution().max_prod_three_nlogn(values) # values may be None # after Solution().max_prod_three_nlogn(values or [])
Defensive patterns
Strategy: type-guard
Validate before calling
if not array:
array = []
best = Solution().max_prod_three_nlogn(array) if array else None Type guard
def is_int_list(x):
return isinstance(x, list) and all(isinstance(v, int) for v in x) Try / catch
try:
Solution().max_prod_three_nlogn(array)
except TypeError as e:
if 'cannot be None' in str(e):
array = []
else:
raise Prevention
- Coalesce None inputs to [] before calling.
- Note the method sorts in place; pass a copy if the original order matters.
When it happens
Trigger: Calling max_prod_three_nlogn(None) or max_prod_three_nlogn() with an uninitialized variable.
Common situations: Optional numeric datasets (empty feed, missing column) flowing into the routine; also beware it mutates the caller's list via array.sort().
Related errors
- array cannot be None
- nums cannot be None
- array cannot be None
- ransom_note or magazine cannot be None
- sentence cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/9074546d21219f09.
Report an issue: GitHub.