{"record":{"id":"2358da3179813384","repo":"donnemartin/interactive-coding-challenges","slug":"stack-is-full","errorCode":null,"errorMessage":"Stack is full","messagePattern":"Stack is full","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"stacks_queues/n_stacks/n_stacks_solution.ipynb","lineNumber":129,"sourceCode":"   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Stacks(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, num_stacks, stack_size):\\n\",\n    \"        self.num_stacks = num_stacks\\n\",\n    \"        self.stack_size = stack_size\\n\",\n    \"        self.stack_pointers = [-1] * self.num_stacks\\n\",\n    \"        self.stack_array = [None] * self.num_stacks * self.stack_size\\n\",\n    \"\\n\",\n    \"    def abs_index(self, stack_index):\\n\",\n    \"        return stack_index * self.stack_size + self.stack_pointers[stack_index]\\n\",\n    \"\\n\",\n    \"    def push(self, stack_index, data):\\n\",\n    \"        if self.stack_pointers[stack_index] == self.stack_size - 1:\\n\",\n    \"            raise Exception('Stack is full')\\n\",\n    \"        self.stack_pointers[stack_index] += 1\\n\",\n    \"        array_index = self.abs_index(stack_index)\\n\",\n    \"        self.stack_array[array_index] = data\\n\",\n    \"\\n\",\n    \"    def pop(self, stack_index):\\n\",\n    \"        if self.stack_pointers[stack_index] == -1:\\n\",\n    \"            raise Exception('Stack is empty')\\n\",\n    \"        array_index = self.abs_index(stack_index)\\n\",\n    \"        data = self.stack_array[array_index]\\n\",\n    \"        self.stack_array[array_index] = None\\n\",\n    \"        self.stack_pointers[stack_index] -= 1\\n\",\n    \"        return data\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/stacks_queues/n_stacks/n_stacks_solution.ipynb#L111-L147","documentation":"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.","triggerScenarios":"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.","commonSituations":"Fixed-capacity buffer sizing in interviews/exercises; forgetting that capacity is per virtual stack, not total; loops that push without tracking counts.","solutions":["Pop from the stack before pushing again, or check space with the pointer/stack_size before pushing","Construct the stacks with a larger stack_size if more elements are needed","Wrap push in try/except and grow/drop on overflow if using this class in real code"],"exampleFix":"# before\nstacks.push(0, item)  # may raise 'Stack is full'\n\n# after\nstack_size = stacks.stack_size\nif stacks.stack_pointers[0] < stack_size - 1:\n    stacks.push(0, item)\nelse:\n    stacks.pop(0)\n    stacks.push(0, item)","handlingStrategy":"validation","validationCode":"if stacks.stack_pointers[stack_index] < stacks.stack_size - 1:\n    stacks.push(stack_index, data)\nelse:\n    raise_or_handle_full(stack_index)","typeGuard":null,"tryCatchPattern":"try:\n    stacks.push(i, item)\nexcept Exception as e:\n    if str(e) == 'Stack is full':\n        stacks.pop(i)  # make room or route elsewhere\n    else:\n        raise","preventionTips":["Size stack_size to the max expected per stack","Track push counts per stack_index","Pop before pushing in bounded buffers"],"tags":["python","stack","overflow","capacity","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"}