donnemartin/interactive-coding-challenges · error · Exception

Stack is full

Error message

Stack is full

What it means

n_stacks' push raises Exception('Stack is full') when the given stack's pointer has reached stack_size - 1, i.e. that virtual stack within the single backing array is at capacity. The implementation divides one array into N fixed-size stacks, so overflow of any one stack cannot be absorbed by neighbors.

Source

Thrown at stacks_queues/n_stacks/n_stacks_solution.ipynb:129

   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Stacks(object):\n",
    "\n",
    "    def __init__(self, num_stacks, stack_size):\n",
    "        self.num_stacks = num_stacks\n",
    "        self.stack_size = stack_size\n",
    "        self.stack_pointers = [-1] * self.num_stacks\n",
    "        self.stack_array = [None] * self.num_stacks * self.stack_size\n",
    "\n",
    "    def abs_index(self, stack_index):\n",
    "        return stack_index * self.stack_size + self.stack_pointers[stack_index]\n",
    "\n",
    "    def push(self, stack_index, data):\n",
    "        if self.stack_pointers[stack_index] == self.stack_size - 1:\n",
    "            raise Exception('Stack is full')\n",
    "        self.stack_pointers[stack_index] += 1\n",
    "        array_index = self.abs_index(stack_index)\n",
    "        self.stack_array[array_index] = data\n",
    "\n",
    "    def pop(self, stack_index):\n",
    "        if self.stack_pointers[stack_index] == -1:\n",
    "            raise Exception('Stack is empty')\n",
    "        array_index = self.abs_index(stack_index)\n",
    "        data = self.stack_array[array_index]\n",
    "        self.stack_array[array_index] = None\n",
    "        self.stack_pointers[stack_index] -= 1\n",
    "        return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Pop from the stack before pushing again, or check space with the pointer/stack_size before pushing
  2. Construct the stacks with a larger stack_size if more elements are needed
  3. Wrap push in try/except and grow/drop on overflow if using this class in real code

Example fix

# before
stacks.push(0, item)  # may raise 'Stack is full'

# after
stack_size = stacks.stack_size
if stacks.stack_pointers[0] < stack_size - 1:
    stacks.push(0, item)
else:
    stacks.pop(0)
    stacks.push(0, item)
Defensive patterns

Strategy: validation

Validate before calling

if stacks.stack_pointers[stack_index] < stacks.stack_size - 1:
    stacks.push(stack_index, data)
else:
    raise_or_handle_full(stack_index)

Try / catch

try:
    stacks.push(i, item)
except Exception as e:
    if str(e) == 'Stack is full':
        stacks.pop(i)  # make room or route elsewhere
    else:
        raise

Prevention

When it happens

Trigger: Calling push(stack_index, data) more than stack_size times on the same stack_index; e.g. stack_size=5 and a sixth push to stack 0 raises. Also when defaulting stack_size too small for the workload.

Common situations: Fixed-capacity buffer sizing in interviews/exercises; forgetting that capacity is per virtual stack, not total; loops that push without tracking counts.

Related errors


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