{"record":{"id":"0baeddae7a95a65b","repo":"krahets/hello-algo","slug":"the-deque-is-empty-0baedd","errorCode":null,"errorMessage":"The Deque Is Empty.","messagePattern":"The Deque Is Empty\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_stack_and_queue/array_deque.ts","lineNumber":88,"sourceCode":"    /* Rear of the queue dequeue */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // Move front pointer backward by one position\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* Access rear of the queue element */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* Driver Code */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // Initialize double-ended queue\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* Return array for printing */\n    toArray(): number[] {\n        // Elements enqueue\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_stack_and_queue/array_deque.ts#L70-L106","documentation":"Thrown by ArrayDeque.peekFirst when the deque is empty. peekFirst reads nums[front]; with queSize === 0 there is no valid element and the index would be stale, so the method throws rather than return garbage. popFirst delegates here, so it propagates the same error.","triggerScenarios":"Calling peekFirst or popFirst before any push; calling after the deque has been fully drained; using front/queSize pointers that desynced from actual inserts.","commonSituations":"Unconditional peek in polling loops; deque used as a work queue with no items queued; off-by-one in size bookkeeping.","solutions":["Check isEmpty() before peekFirst/popFirst.","Drain with while (!deque.isEmpty()).","Wrap in a helper that returns undefined on empty."],"exampleFix":"// before\nconst head = deque.peekFirst(); // throws if empty\n\n// after\nconst head = deque.isEmpty() ? undefined : deque.peekFirst();","handlingStrategy":"validation","validationCode":"const head = deque.isEmpty() ? undefined : deque.peekFirst();","typeGuard":"function hasElements(d) { return typeof d.isEmpty === 'function' && !d.isEmpty(); }","tryCatchPattern":"try { return deque.peekFirst(); }\ncatch (e) { if (!/Deque Is Empty/.test(e.message)) throw e; return undefined; }","preventionTips":["Check isEmpty() before any peek/pop from the front.","Drain with while (!deque.isEmpty()).","Prefer a wrapper returning undefined on empty for pipeline code."],"tags":["deque","typescript","validation","guard-clause","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}