{"record":{"id":"7176357ec0efc2d8","repo":"krahets/hello-algo","slug":"stack-is-empty-717635","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts","lineNumber":39,"sourceCode":"    }\n\n    /* Check if the stack is empty */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Push */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* Pop */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('Stack is empty');\n        return this.stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts#L21-L57","documentation":"Thrown by LinkedListStack.pop when stackPeek is null after peek. As with the queue case, this is a redundant defensive guard: pop calls peek() first, which already throws 'Stack is empty' when stackPeek is null. This branch is only reachable if stkSize and stackPeek are inconsistent. Treat it as the same empty-stack precondition as error 75.","triggerScenarios":"Popping an empty stack (normally surfaced via peek first); corrupted state where stkSize > 0 but stackPeek is null.","commonSituations":"Calling pop without checking the stack depth; manual mutation of internal pointers; unbalanced push/pop in algorithm code.","solutions":["Check size() === 0 (or isEmpty if exposed) before pop.","Drain with while (stack.size() > 0).","Only mutate the stack through its public API."],"exampleFix":"// before\nconst v = stack.pop(); // throws if empty\n\n// after\nconst v = stack.size() === 0 ? undefined : stack.pop();","handlingStrategy":"validation","validationCode":"const v = stack.size() === 0 ? undefined : stack.pop();","typeGuard":"function hasElements(s) { return typeof s.size === 'function' && s.size() > 0; }","tryCatchPattern":"try { return stack.pop(); }\ncatch (e) { if (!/Stack is empty/.test(e.message)) throw e; return undefined; }","preventionTips":["Check size() before pop.","Drain with while (stack.size() > 0).","Mutate internal pointers only via the public API to keep stkSize/stackPeek consistent."],"tags":["stack","typescript","validation","guard-clause","empty-state","redundant-guard"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}