krahets/hello-algo · error · Error
The Deque Is Empty.
Error message
The Deque Is Empty.
What it means
Thrown by ArrayDeque.peekFirst (JS) with message 'The Deque Is Empty.' when reading the front element of an empty deque. peekFirst underlies popFirst, so the same throw propagates from popFirst on an empty deque.
Source
Thrown at ru/codes/javascript/chapter_stack_and_queue/array_deque.js:88
/* Извлечение из головы очереди */
popFirst() {
const num = this.peekFirst();
// Указатель головы сдвигается на одну позицию назад
this.#front = this.index(this.#front + 1);
this.#queSize--;
return num;
}
/* Извлечение из хвоста очереди */
popLast() {
const num = this.peekLast();
this.#queSize--;
return num;
}
/* Доступ к элементу в начале очереди */
peekFirst() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
return this.#nums[this.#front];
}
/* Доступ к элементу в конце очереди */
peekLast() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
// Вычислить индекс хвостового элемента
const last = this.index(this.#front + this.#queSize - 1);
return this.#nums[last];
}
/* Вернуть массив для вывода */
toArray() {
// Преобразовывать только элементы списка в пределах фактической длины
const res = [];
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)
Solutions
- Guard with isEmpty(): if (!deque.isEmpty()) deque.popFirst().
- Use peekFirst() to test before consuming.
- Track expected element counts on the producer side.
Example fix
// before const x = deque.popFirst(); // throws when empty // after const x = deque.isEmpty() ? null : deque.popFirst();
Defensive patterns
Strategy: validation
Validate before calling
const x = deque.isEmpty() ? null : deque.popFirst();
Try / catch
try { deque.popFirst(); } catch (e) { if (e.message !== 'The Deque Is Empty.') throw e; } Prevention
- Gate every popFirst/peekFirst on isEmpty().
- In producer/consumer code, backpressure on empty rather than over-popping.
- Return a sentinel from a wrapper to keep call sites clean.
When it happens
Trigger: Calling peekFirst() (or popFirst(), which delegates to it) when #queSize === 0.
Common situations: Treating the deque as a stack/queue and popping past empty; consumer/producer imbalance where consumers drain faster than producers fill.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/a098711e1e460e64.
Report an issue: GitHub.