krahets/hello-algo · error · Error
The Deque Is Empty.
Error message
The Deque Is Empty.
What it means
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.
Solutions
- Check isEmpty() before peekFirst/popFirst.
- Drain with while (!deque.isEmpty()).
- Wrap in a helper that returns undefined on empty.
Example fix
// before const head = deque.peekFirst(); // throws if empty // after const head = deque.isEmpty() ? undefined : deque.peekFirst();
Defensive patterns
Strategy: validation
Validate before calling
const head = deque.isEmpty() ? undefined : deque.peekFirst();
Type guard
function hasElements(d) { return typeof d.isEmpty === 'function' && !d.isEmpty(); } Try / catch
try { return deque.peekFirst(); }
catch (e) { if (!/Deque Is Empty/.test(e.message)) throw e; return undefined; } Prevention
- Check isEmpty() before any peek/pop from the front.
- Drain with while (!deque.isEmpty()).
- Prefer a wrapper returning undefined on empty for pipeline code.
When it happens
Trigger: Calling peekFirst or popFirst before any push; calling after the deque has been fully drained; using front/queSize pointers that desynced from actual inserts.
Common situations: Unconditional peek in polling loops; deque used as a work queue with no items queued; off-by-one in size bookkeeping.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/0baeddae7a95a65b.
Report an issue: GitHub.
Appendix: source
Thrown at en/codes/typescript/chapter_stack_and_queue/array_deque.ts:88
/* Rear of the queue dequeue */
popFirst(): number {
const num: number = this.peekFirst();
// Move front pointer backward by one position
this.front = this.index(this.front + 1);
this.queSize--;
return num;
}
/* Access rear of the queue element */
popLast(): number {
const num: number = this.peekLast();
this.queSize--;
return num;
}
/* Return list for printing */
peekFirst(): number {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
return this.nums[this.front];
}
/* Driver Code */
peekLast(): number {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
// Initialize double-ended queue
const last = this.index(this.front + this.queSize - 1);
return this.nums[last];
}
/* Return array for printing */
toArray(): number[] {
// Elements enqueue
const res: number[] = [];
for (let i = 0, j = this.front; i < this.queSize; i++, j++) {
res[i] = this.nums[this.index(j)];
}View on GitHub (pinned to 69932aed18)