donnemartin/interactive-coding-challenges · error · TypeError

array cannot be None or empty

Error message

array cannot be None or empty

What it means

Bits.new_int raises TypeError('array cannot be None or empty') because the algorithm maps input integers into a BitArray bit vector and then scans for the first unset bit; with no input there is nothing to map and the result would be meaningless. The 'not array' check rejects both None and empty lists.

Source

Thrown at sorting_searching/new_int/new_int_solution.ipynb:110

   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bitstring import BitArray  # Run pip install bitstring\n",
    "\n",
    "\n",
    "class Bits(object):\n",
    "\n",
    "    def new_int(self, array, max_size):\n",
    "        if not array:\n",
    "            raise TypeError('array cannot be None or empty')\n",
    "        bit_vector = BitArray(max_size)\n",
    "        for item in array:\n",
    "            bit_vector[item] = True\n",
    "        for index, item in enumerate(bit_vector):\n",
    "            if not item:\n",
    "                return index\n",
    "        return None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "code",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Verify the array is populated before calling new_int; skip the call for empty inputs
  2. Check the upstream producer of the array (loop/file read) to see why it yielded no items
  3. If an empty array legitimately means 'answer is 0', handle that case in the caller instead of calling new_int

Example fix

// before
index = Bits().new_int(array, max_size)  # array may be []

// after
index = Bits().new_int(array, max_size) if array else 0
Defensive patterns

Strategy: validation

Validate before calling

if not array:
    # nothing to map; first free index is 0 by convention
    return 0
index = Bits().new_int(array, max_size)

Type guard

def has_items(x):
    return isinstance(x, list) and len(x) > 0

Try / catch

try:
    index = bits.new_int(array, max_size)
except TypeError:
    index = 0  # empty/None input treated as answer 0

Prevention

When it happens

Trigger: Calling Bits().new_int(None, max_size) or Bits().new_int([], max_size). Also triggered when max_size is omitted/misused so the array argument ends up empty.

Common situations: Feeding an empty result set from a previous computation into the missing-integer finder; optional function parameters defaulting to None; test harnesses checking boundary conditions.

Related errors


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