donnemartin/interactive-coding-challenges · error · TypeError

array or val cannot be None

Error message

array or val cannot be None

What it means

Array.search_sorted_array raises TypeError('array or val cannot be None') if either the array to search or the target value is None. The binary-search-style recursion needs both a valid array and a comparable target; None for either is a caller bug, while an empty array is legal and returns None.

Source

Thrown at sorting_searching/rotated_array_search/rotated_array_search_solution.ipynb:140

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Array(object):\n",
    "\n",
    "    def search_sorted_array(self, array, val):\n",
    "        if array is None or val is None:\n",
    "            raise TypeError('array or val cannot be None')\n",
    "        if not array:\n",
    "            return None\n",
    "        return self._search_sorted_array(array, val, start=0, end=len(array) - 1)\n",
    "\n",
    "    def _search_sorted_array(self, array, val, start, end):\n",
    "        if end < start:\n",
    "            return None\n",
    "        mid = (start + end) // 2\n",
    "        if array[mid] == val:\n",
    "            return mid\n",
    "        # Left side is sorted\n",
    "        if array[start] < array[mid]:\n",
    "            if array[start] <= val < array[mid]:\n",
    "                return self._search_sorted_array(array, val, start, mid - 1)\n",
    "            else:\n",
    "                return self._search_sorted_array(array, val, mid + 1, end)\n",
    "        # Right side is sorted\n",
    "        elif array[start] > array[mid]:\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Check both arguments before calling: skip or default when array or val is None
  2. Fix the source of the None val (e.g. dict.get(key, default) or validate user input)
  3. For optional search targets, only invoke the search when val is known

Example fix

// before
result = Array().search_sorted_array(arr, val)  # val may be None

// after
result = Array().search_sorted_array(arr, val) if val is not None else None
Defensive patterns

Strategy: validation

Validate before calling

if array is None or val is None:
    return None  # or raise your own clearer error
return Array().search_sorted_array(array, val)

Type guard

def can_search(arr, v):
    return isinstance(arr, list) and arr and v is not None

Try / catch

try:
    idx = a.search_sorted_array(array, val)
except TypeError as e:
    if 'cannot be None' in str(e):
        idx = None
    else:
        raise

Prevention

When it happens

Trigger: Calling search_sorted_array(None, 5) or search_sorted_array([1,2,3], None); commonly the val comes from a dict .get() or user input that returned None.

Common situations: Lookups keyed by optional identifiers (dict.get returning None, missing query params); test cases for boundary conditions; passing an uninitialized search target.

Related errors


AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28). Data as JSON: /api/errors/1e193056b9a72f33. Report an issue: GitHub.