donnemartin/interactive-coding-challenges · error · TypeError
array cannot be None
Error message
array cannot be None
What it means
Raised by Solution.merge_ranges when the array argument is None; iterating/sorting None would crash with a less clear TypeError, so the method fails fast with an explicit message. None is treated as a programming/caller error, distinct from an empty list which returns [].
Source
Thrown at online_judges/merge_ranges/merge_ranges_solution.ipynb:140
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def merge_ranges(self, array):\n",
" if array is None:\n",
" raise TypeError('array cannot be None')\n",
" if not array:\n",
" return array\n",
" sorted_array = sorted(array)\n",
" merged_array = [sorted_array[0]]\n",
" for index, item in enumerate(sorted_array):\n",
" if index == 0:\n",
" continue\n",
" start_prev, end_prev = merged_array[-1]\n",
" start_curr, end_curr = item\n",
" if end_prev < start_curr:\n",
" # No overlap, add the entry\n",
" merged_array.append(item)\n",
" else:\n",
" # Overlap, update the previous entry's end value\n",
" merged_array[-1] = (start_prev, max(end_prev, end_curr))\n",
" return merged_array"
]
},View on GitHub (pinned to 358f2cc604)
Solutions
- Initialize the argument to [] instead of None when data may be empty.
- Guard at the call site: merge_ranges(ranges or []).
- Catch TypeError if None can legitimately flow through and handle it.
Example fix
# before merged = Solution().merge_ranges(meetings) # meetings may be None # after merged = Solution().merge_ranges(meetings or [])
Defensive patterns
Strategy: type-guard
Validate before calling
ranges = ranges if ranges is not None else [] merged = Solution().merge_ranges(ranges)
Type guard
def is_range_list(x):
return isinstance(x, list) and all(isinstance(r, (list, tuple)) and len(r) == 2 for r in x) Try / catch
try:
merged = Solution().merge_ranges(ranges)
except TypeError as e:
if 'cannot be None' in str(e):
merged = []
else:
raise Prevention
- Never use None as the empty-input sentinel; use [].
- Coalesce optional values with 'or []' before passing.
When it happens
Trigger: Calling merge_ranges(None) or passing a variable that was initialized to None and never assigned (e.g. a failed fetch of meeting ranges).
Common situations: Optional upstream data (CSV column, API response field) that is None when missing, or a default parameter of None used as a sentinel.
Related errors
- nums cannot be None
- array 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/dbac563f3e85a6c6.
Report an issue: GitHub.