krahets/hello-algo · error · Error
очередь пуста
Error message
очередь пуста
What it means
Thrown by ArrayQueue.peek (JS, array_queue.js) with message 'очередь пуста' ('queue is empty') when reading the head of an empty circular queue. pop() calls peek() first, so the same throw propagates from pop() on an empty queue.
Source
Thrown at ru/codes/javascript/chapter_stack_and_queue/array_queue.js:57
// С помощью операции взятия по модулю вернуть rear к началу после выхода за конец массива
const rear = (this.#front + this.size) % this.capacity;
// Добавить num в хвост очереди
this.#nums[rear] = num;
this.#queSize++;
}
/* Извлечь из очереди */
pop() {
const num = this.peek();
// Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива
this.#front = (this.#front + 1) % this.capacity;
this.#queSize--;
return num;
}
/* Доступ к элементу в начале очереди */
peek() {
if (this.isEmpty()) throw new Error('очередь пуста');
return this.#nums[this.#front];
}
/* Вернуть Array */
toArray() {
// Преобразовывать только элементы списка в пределах фактической длины
const arr = new Array(this.size);
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
arr[i] = this.#nums[j % this.capacity];
}
return arr;
}
}
/* Driver Code */
/* Инициализация очереди */
const capacity = 10;
const queue = new ArrayQueue(capacity);View on GitHub (pinned to 69932aed18)
Solutions
- Check isEmpty() (or size === 0) before pop/peek.
- In BFS, gate frontier expansion on the queue not being empty.
- Capture size once when draining: for (let n = q.size(); n > 0; n--) q.pop().
Example fix
// before
while (q.size() >= 0) { q.pop(); } // throws on final empty pop
// after
while (!q.isEmpty()) { const v = q.pop(); } Defensive patterns
Strategy: validation
Validate before calling
while (!q.isEmpty()) { const v = q.pop(); process(v); } Try / catch
try { q.pop(); } catch (e) { if (e.message !== 'очередь пуста') throw e; } Prevention
- Use while (!q.isEmpty()) for drain loops, not while (q.size() >= 0).
- In BFS, gate frontier expansion on non-empty queue.
- Capture size once when draining a known count.
When it happens
Trigger: Calling peek() or pop() when #queSize === 0. Common in producer/consumer loops that over-consume.
Common situations: BFS exhaustion; worker loop draining a task queue to empty; mismatch between enqueue and dequeue counts.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/171735ce5681f794.
Report an issue: GitHub.