krahets/hello-algo · error · Error
стек пуст
Error message
стек пуст
What it means
Thrown by pop() on an array-backed stack when isEmpty() is true. The message ('стек пуст', Russian for 'stack is empty') guards the underlying Array.pop so the method can return a concrete number instead of undefined.
Source
Thrown at ru/codes/typescript/chapter_stack_and_queue/array_stack.ts:31
/* Получение длины стека */
get size(): number {
return this.stack.length;
}
/* Проверка, пуст ли стек */
isEmpty(): boolean {
return this.stack.length === 0;
}
/* Поместить в стек */
push(num: number): void {
this.stack.push(num);
}
/* Извлечь из стека */
pop(): number | undefined {
if (this.isEmpty()) throw new Error('стек пуст');
return this.stack.pop();
}
/* Доступ к верхнему элементу стека */
top(): number | undefined {
if (this.isEmpty()) throw new Error('стек пуст');
return this.stack[this.stack.length - 1];
}
/* Вернуть Array */
toArray() {
return this.stack;
}
}
/* Driver Code */
/* Инициализация стека */
const stack = new ArrayStack();View on GitHub (pinned to 69932aed18)
Solutions
- Check stack.isEmpty() before pop().
- Use while (!stack.isEmpty()) for full drains, or bound pops by stack.size().
- Catch the error when an empty pop is a recoverable condition.
- Audit push/pop pairing in the algorithm to ensure balance.
Example fix
// before const top = stack.pop(); // throws if empty // after const top = stack.isEmpty() ? undefined : stack.pop();
Defensive patterns
Strategy: validation
Validate before calling
if (!stack.isEmpty()) {
const top = stack.pop();
} Try / catch
try {
const top = stack.pop();
} catch (e) {
if (e instanceof Error && e.message === 'стек пуст') {
// empty stack; handle gracefully
} else throw e;
} Prevention
- Check isEmpty() before pop().
- Audit push/pop pairing in evaluators and DFS code.
- Bound drain loops by stack.size() or use while (!isEmpty()).
When it happens
Trigger: Calling pop() more times than push() was called; popping a freshly constructed empty stack; an unbalanced push/pop sequence in expression evaluation or backtracking.
Common situations: DFS/recursion simulation that pops past the root; bracket-matching or undo logic that under-counts the stack depth; popping inside a loop without a size bound.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/5431d07961d07c8a.
Report an issue: GitHub.