{"record":{"id":"86a9b1bb97b067e0","repo":"donnemartin/interactive-coding-challenges","slug":"stack-full","errorCode":null,"errorMessage":"Stack full","messagePattern":"Stack full","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb","lineNumber":124,"sourceCode":"    \"%run ../stack/stack.py\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class StackWithCapacity(Stack):\\n\",\n    \"\\n\",\n    \"    def __init__(self, top=None, capacity=10):\\n\",\n    \"        super(StackWithCapacity, self).__init__(top)\\n\",\n    \"        self.capacity = capacity\\n\",\n    \"        self.num_items = 0\\n\",\n    \"\\n\",\n    \"    def push(self, data):\\n\",\n    \"        if self.is_full():\\n\",\n    \"            raise Exception('Stack full')\\n\",\n    \"        super(StackWithCapacity, self).push(data)\\n\",\n    \"        self.num_items += 1\\n\",\n    \"\\n\",\n    \"    def pop(self):\\n\",\n    \"        self.num_items -= 1\\n\",\n    \"        return super(StackWithCapacity, self).pop()\\n\",\n    \"\\n\",\n    \"    def is_full(self):\\n\",\n    \"        return self.num_items == self.capacity\\n\",\n    \"\\n\",\n    \"    def is_empty(self):\\n\",\n    \"        return self.num_items == 0\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class SetOfStacks(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, indiv_stack_capacity):\\n\",\n    \"        self.indiv_stack_capacity = indiv_stack_capacity\\n\",","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/stacks_queues/set_of_stacks/set_of_stacks_solution.ipynb#L106-L142","documentation":"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.","triggerScenarios":"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).","commonSituations":"Subclassing/altering SetOfStacks (a common interview extension) and breaking the new-stack rollover; unit tests hitting the inner class directly; setting capacity too small.","solutions":["Push through SetOfStacks.push (which spawns a new stack on full) rather than StackWithCapacity.push directly","If subclassing, preserve/override is_full and rollover behavior consistently","Increase the capacity passed when constructing the stacks"],"exampleFix":"# before\nsub_stack.push(item)  # raises 'Stack full' past capacity\n\n# after\nset_of_stacks.push(item)  # SetOfStacks rolls over to a new sub-stack automatically","handlingStrategy":"validation","validationCode":"if not stack.is_full():\n    stack.push(data)\nelse:\n    new_stack = StackWithCapacity(stack.capacity)\n    new_stack.push(data)","typeGuard":null,"tryCatchPattern":"try:\n    stack.push(data)\nexcept Exception as e:\n    if str(e) == 'Stack full':\n        raise_or_create_new_stack(data)\n    else:\n        raise","preventionTips":["Push via SetOfStacks.push so rollover happens automatically","Set capacity above the maximum expected stack depth","Don't bypass the SetOfStacks wrapper when subclassing"],"tags":["python","stack","capacity","overflow","data-structure"],"backgroundTag":"stack-overflow-fixed-capacity","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}