donnemartin/interactive-coding-challenges · error · TypeError

array cannot be None

Error message

array cannot be None

What it means

Raised by the brute-force variant Solution.mult_other_numbers_brute when array is None. It builds, for each index, the product of every other element; a None input cannot be iterated, so the method fails fast with TypeError('array cannot be None').

Source

Thrown at online_judges/mult_other_numbers/mult_other_numbers_solution.ipynb:140

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "\n",
    "    def mult_other_numbers_brute(self, array):\n",
    "        if array is None:\n",
    "            raise TypeError('array cannot be None')\n",
    "        if not array:\n",
    "            return array\n",
    "        if len(array) == 1:\n",
    "            return []\n",
    "        result = []\n",
    "        for i in range(len(array)):\n",
    "            curr_sum = 1\n",
    "            for j in range(len(array)):\n",
    "                if i == j:\n",
    "                    continue\n",
    "                curr_sum *= array[j]\n",
    "            result.append(curr_sum)\n",
    "        return result\n",
    "\n",
    "    def mult_other_numbers(self, array):\n",
    "        if array is None:\n",
    "            raise TypeError('array cannot be None')\n",
    "        if not array:\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Coalesce to a list: mult_other_numbers_brute(array or []).
  2. Fix the loader so it returns [] on empty/failed input instead of None.
  3. Catch TypeError around the call for defensive top-level handling.

Example fix

# before
result = Solution().mult_other_numbers_brute(data)  # data may be None

# after
result = Solution().mult_other_numbers_brute(data or [])
Defensive patterns

Strategy: type-guard

Validate before calling

array = array or []
result = Solution().mult_other_numbers_brute(array)

Type guard

def is_int_list(x):
    return isinstance(x, list) and all(isinstance(v, int) for v in x)

Try / catch

try:
    Solution().mult_other_numbers_brute(array)
except TypeError as e:
    if 'cannot be None' in str(e):
        array = []
    else:
        raise

Prevention

When it happens

Trigger: Calling mult_other_numbers_brute(None), or sharing one optional input variable between the brute and optimized variants without null-checking.

Common situations: Benchmarking both implementations with the same dataset loader that can return None on parse failure.

Related errors


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