{"record":{"id":"32b148f0ee29a328","repo":"krahets/hello-algo","slug":"queue-is-empty-32b148","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts","lineNumber":49,"sourceCode":"    push(num: number): void {\n        // Add num after the tail node\n        const node = new ListNode(num);\n        // If the queue is empty, make both front and rear point to the node\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // If the queue is not empty, add the node after the tail node\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('Queue is empty');\n        // Delete head node\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.front!.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts#L31-L67","documentation":"Thrown by LinkedListQueue.pop when front is null after peek. In practice this is a redundant/defensive guard: pop first calls peek(), which already throws 'Queue is empty' when size === 0, so this branch is effectively unreachable unless size and front fall out of sync. Treat it as the same empty-queue precondition as error 73.","triggerScenarios":"Popping an empty queue (normally surfaced via peek first); a corrupted state where queSize > 0 but front is null (should not happen in normal use).","commonSituations":"Calling pop without an emptiness check; consumer loops outrunning producers; internal pointer corruption from manual list mutation.","solutions":["Check size() === 0 (or isEmpty if exposed) before pop.","Drain with while (queue.size() > 0).","Avoid mutating front/rear/queSize outside the class API."],"exampleFix":"// before\nconst v = queue.pop(); // throws if empty\n\n// after\nconst v = queue.size() === 0 ? undefined : queue.pop();","handlingStrategy":"validation","validationCode":"const v = queue.size() === 0 ? undefined : queue.pop();","typeGuard":"function hasElements(q) { return typeof q.size === 'function' && q.size() > 0; }","tryCatchPattern":"try { return queue.pop(); }\ncatch (e) { if (!/Queue is empty/.test(e.message)) throw e; return undefined; }","preventionTips":["Check size() before pop.","Drain with while (queue.size() > 0).","Only mutate front/rear/queSize through the public API to avoid size/pointer desync."],"tags":["queue","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"}