donnemartin/interactive-coding-challenges · error · TypeError
greed_indices or cookie_sizes cannot be None
Error message
greed_indices or cookie_sizes cannot be None
What it means
Raised by Solution.find_content_children (LeetCode 'Assign Cookies') when greed_indices or cookie_sizes is None. Both lists are sorted and iterated, so None would crash with a confusing AttributeError; the guard makes the list contract explicit.
Source
Thrown at online_judges/assign_cookies/assign_cookies_solution.ipynb:128
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def find_content_children(self, greed_indices, cookie_sizes):\n",
" if greed_indices is None or cookie_sizes is None:\n",
" raise TypeError('greed_indices or cookie_sizes cannot be None')\n",
" if not greed_indices or not cookie_sizes:\n",
" return 0\n",
" greed_indices.sort()\n",
" cookie_sizes.sort()\n",
" greed_index = 0\n",
" num_children = 0\n",
" for size in cookie_sizes:\n",
" if greed_index >= len(greed_indices):\n",
" break\n",
" if size >= greed_indices[greed_index]:\n",
" num_children += 1\n",
" greed_index += 1\n",
" return num_children"
]
},
{
"cell_type": "markdown",
"metadata": {},View on GitHub (pinned to 358f2cc604)
Solutions
- Pass two lists (possibly empty — empty lists correctly return 0)
- Initialize variables to [] instead of None
- Check for None and default to empty list before the call
Example fix
# before
solution.find_content_children(payload.get('g'), payload.get('s'))
# after
solution.find_content_children(payload.get('g') or [], payload.get('s') or []) Defensive patterns
Strategy: validation
Validate before calling
greed_indices = greed_indices or [] cookie_sizes = cookie_sizes or [] solution.find_content_children(greed_indices, cookie_sizes)
Type guard
def is_list(x): return isinstance(x, list)
Try / catch
try:
n = solution.find_content_children(g, s)
except TypeError:
n = 0 Prevention
- Initialize list variables to [] not None
- Use `or []` when arrays come from nullable payloads
When it happens
Trigger: Calling find_content_children(None, [1,2]) or with either list None — often from an optional argument, an empty initialization like greed = None, or a missing dict key.
Common situations: Test cases asserting the None guard; adapters receiving possibly-missing arrays from JSON payloads.
Related errors
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/f397d3bb41b281b3.
Report an issue: GitHub.