donnemartin/interactive-coding-challenges · error · Exception

Stack full

Error message

Stack full

What it means

SetOfStacks' inner StackWithCapacity.push raises Exception('Stack full') when is_full() reports num_items == capacity. Unlike a plain stack, this class wraps a set of fixed-capacity stacks; the surrounding SetOfStacks normally handles rollover to a new stack, so hitting this error usually means you pushed directly on a full StackWithCapacity or the rollover logic was bypassed.

Source

Thrown at stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb:124

    "%run ../stack/stack.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StackWithCapacity(Stack):\n",
    "\n",
    "    def __init__(self, top=None, capacity=10):\n",
    "        super(StackWithCapacity, self).__init__(top)\n",
    "        self.capacity = capacity\n",
    "        self.num_items = 0\n",
    "\n",
    "    def push(self, data):\n",
    "        if self.is_full():\n",
    "            raise Exception('Stack full')\n",
    "        super(StackWithCapacity, self).push(data)\n",
    "        self.num_items += 1\n",
    "\n",
    "    def pop(self):\n",
    "        self.num_items -= 1\n",
    "        return super(StackWithCapacity, self).pop()\n",
    "\n",
    "    def is_full(self):\n",
    "        return self.num_items == self.capacity\n",
    "\n",
    "    def is_empty(self):\n",
    "        return self.num_items == 0\n",
    "\n",
    "\n",
    "class SetOfStacks(object):\n",
    "\n",
    "    def __init__(self, indiv_stack_capacity):\n",
    "        self.indiv_stack_capacity = indiv_stack_capacity\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Push through SetOfStacks.push (which spawns a new stack on full) rather than StackWithCapacity.push directly
  2. If subclassing, preserve/override is_full and rollover behavior consistently
  3. Increase the capacity passed when constructing the stacks

Example fix

# before
sub_stack.push(item)  # raises 'Stack full' past capacity

# after
set_of_stacks.push(item)  # SetOfStacks rolls over to a new sub-stack automatically
Defensive patterns

Strategy: validation

Validate before calling

if not stack.is_full():
    stack.push(data)
else:
    new_stack = StackWithCapacity(stack.capacity)
    new_stack.push(data)

Try / catch

try:
    stack.push(data)
except Exception as e:
    if str(e) == 'Stack full':
        raise_or_create_new_stack(data)
    else:
        raise

Prevention

When it happens

Trigger: Pushing more than 'capacity' items onto a StackWithCapacity directly instead of through SetOfStacks.push; or using SetOfStacks whose push failed to create a new sub-stack (custom subclass overriding push incorrectly).

Common situations: Subclassing/altering SetOfStacks (a common interview extension) and breaking the new-stack rollover; unit tests hitting the inner class directly; setting capacity too small.

Related errors


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