{"record":{"id":"ce531b7890502a08","repo":"krahets/hello-algo","slug":"the-deque-is-empty-ce531b","errorCode":null,"errorMessage":"The Deque Is Empty.","messagePattern":"The Deque Is Empty\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_stack_and_queue/array_deque.js","lineNumber":88,"sourceCode":"    /* Rear of the queue dequeue */\n    popFirst() {\n        const num = 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() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* Driver Code */\n    peekLast() {\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() {\n        // Elements enqueue\n        const res = [];\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/javascript/chapter_stack_and_queue/array_deque.js#L70-L106","documentation":"Thrown by peekFirst() on an array-backed deque when the deque is empty. peekFirst() reads the front element without removing it; popFirst() delegates to it. The guard prevents reading this.#nums[this.#front] on an empty structure where the index is meaningless.","triggerScenarios":"Calling peekFirst() or popFirst() on an empty deque; reading the front before any push; dequeuing more than was enqueued.","commonSituations":"BFS/level-order traversal peeking at empty frontier; producer-consumer where consumer races ahead; calling popFirst in a loop without an emptiness guard.","solutions":["Check deque.isEmpty() before peekFirst()/popFirst().","In a consume loop use while (!deque.isEmpty()) { const v = deque.popFirst(); ... }.","Return a sentinel/Option from a wrapper if empty-peek is a valid application state.","Track enqueue/dequeue counts if you need to bound a fixed number of operations."],"exampleFix":"// before\nconst front = deque.peekFirst(); // throws when empty\n\n// after\nconst front = deque.isEmpty() ? null : deque.peekFirst();","handlingStrategy":"validation","validationCode":"if (!deque.isEmpty()) {\n  const front = deque.peekFirst();\n} else {\n  // handle empty deque\n}","typeGuard":"function dequeHasFront(deque) {\n  return typeof deque.isEmpty === 'function' && !deque.isEmpty();\n}","tryCatchPattern":"try {\n  const front = deque.peekFirst();\n} catch (e) {\n  if (e.message === 'The Deque Is Empty.') { /* empty */ }\n  else throw e;\n}","preventionTips":["Check isEmpty() before peekFirst()/popFirst().","Drain with while (!deque.isEmpty()) popFirst().","Wrap peek in a helper returning null for empty if that is a valid state."],"tags":["deque","empty-state","circular-array","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}