{"record":{"id":"cbef0fc8c21985b7","repo":"donnemartin/interactive-coding-challenges","slug":"stack-is-empty","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"stacks_queues/n_stacks/n_stacks_solution.ipynb","lineNumber":136,"sourceCode":"    \"    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\": [\n    \"## Unit Test\\n\",\n    \"\\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/stacks_queues/n_stacks/n_stacks_solution.ipynb#L118-L154","documentation":"n_stacks' pop raises Exception('Stack is empty') when the stack pointer for that stack_index is -1, meaning no elements have been pushed (or all were popped) on that virtual stack. It prevents reading uninitialized slots of the backing array.","triggerScenarios":"Calling pop(stack_index) on a freshly created stack, or popping more times than you pushed on that same stack_index. Each of the N stacks tracks underflow independently.","commonSituations":"Mismatched push/pop pairing across code paths; popping in a loop without checking remaining elements; mixing up stack indices so you pop a stack you never pushed to.","solutions":["Track or check emptiness before popping: peek at stack_pointers[stack_index] == -1","Ensure every pop has a corresponding successful push for the same stack_index","Wrap pop in try/except Exception and treat underflow as a no-op or signal if unavoidable"],"exampleFix":"# before\ndata = stacks.pop(0)  # may raise 'Stack is empty'\n\n# after\nif stacks.stack_pointers[0] != -1:\n    data = stacks.pop(0)\nelse:\n    data = None","handlingStrategy":"validation","validationCode":"if stacks.stack_pointers[stack_index] != -1:\n    data = stacks.pop(stack_index)\nelse:\n    data = None","typeGuard":null,"tryCatchPattern":"try:\n    data = stacks.pop(i)\nexcept Exception as e:\n    if str(e) == 'Stack is empty':\n        data = None  # treat underflow as empty\n    else:\n        raise","preventionTips":["Check the stack pointer before popping","Keep push/pop calls paired per stack_index","Loop with an emptiness condition rather than exception-driven control flow"],"tags":["python","stack","underflow","data-structure"],"backgroundTag":"stack-underflow-empty-pop","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}