donnemartin/interactive-coding-challenges · error · TypeError

seq cannot be None

Error message

seq cannot be None

What it means

Raised by Subsequence.longest_inc_subseq when seq is None. The function immediately allocates temp and prev arrays of length len(seq), so an explicit TypeError is thrown first with a descriptive message instead of the cryptic len(None) failure.

Source

Thrown at recursion_dynamic/longest_inc_subseq/longest_inc_subseq_solution.ipynb:115

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Subsequence(object):\n",
    "\n",
    "    def longest_inc_subseq(self, seq):\n",
    "        if seq is None:\n",
    "            raise TypeError('seq cannot be None')\n",
    "        if not seq:\n",
    "            return []\n",
    "        temp = [1] * len(seq)\n",
    "        prev = [None] * len(seq)\n",
    "        for r in range(1, len(seq)):\n",
    "            for l in range(r):\n",
    "                if seq[l] < seq[r]:\n",
    "                    if temp[r] < temp[l] + 1:\n",
    "                        temp[r] = temp[l] + 1\n",
    "                        prev[r] = l\n",
    "        max_val = 0\n",
    "        max_index = -1\n",
    "        results = []\n",
    "        for index, value in enumerate(temp):\n",
    "            if value > max_val:\n",
    "                max_val = value\n",
    "                max_index = index\n",
    "        curr_index = max_index\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Default the argument: seq = seq or [] if an empty result is acceptable
  2. Guard the call: if seq is not None: ... else handle empty case
  3. Fix producers so pipelines return empty lists, never None

Example fix

// before
result = sub.longest_inc_subseq(maybe_none_seq)
// after
result = sub.longest_inc_subseq(maybe_none_seq or [])
Defensive patterns

Strategy: validation

Validate before calling

if seq is None:
    seq = []
sub.longest_inc_subseq(seq)

Type guard

def is_seq(x):
    return isinstance(x, (list, tuple))

Try / catch

try:
    sub.longest_inc_subseq(seq)
except TypeError:
    result = []

Prevention

When it happens

Trigger: Calling longest_inc_subseq(None). An empty sequence [] is handled and returns [] — only None raises.

Common situations: Passing a list built by a filter/map chain that can be None; sequence derived from an API response with a missing array field; optional parameters left as None.

Related errors


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