This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
The most recent unfinished thing is the next one to finish.
The question to ask
Does each new element only interact with the most recent unresolved one?
A stack fits whenever the input nests: brackets, function calls, directories, expressions, collisions where the newest item interacts with the one just before it. Push when something opens, pop when it closes, and the top of the stack is always the innermost thing still open. When the question involves undoing, matching or evaluating in order, it's usually a stack.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n) · Space O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Numbers go on the stack; an operator pops two, applies, and pushes the result. Note the order: the first pop is the right operand.
function evalRPN(tokens: string[]): number {
const values: number[] = [];
for (const token of tokens) {
if (!["+", "-", "*", "/"].includes(token)) {
values.push(Number(token));
continue;
}
const right = values.pop() as number;
const left = values.pop() as number;
if (token === "+") values.push(left + right);
else if (token === "-") values.push(left - right);
else if (token === "*") values.push(left * right);
else values.push(Math.trunc(left / right));
}
return values[0];
}Decode String: on [ push the string built so far and the repeat count, then start fresh. On ] pop them and wrap the inner string. Any nested-grammar question has this shape.
function decodeString(s: string): string {
const saved: [string, number][] = [];
let current = "";
let count = 0;
for (const ch of s) {
if (ch >= "0" && ch <= "9") count = count * 10 + Number(ch);
else if (ch === "[") {
saved.push([current, count]);
current = "";
count = 0;
} else if (ch === "]") {
const [before, times] = saved.pop() as [string, number];
current = before + current.repeat(times);
} else current += ch;
}
return current;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given a string of brackets ()[]{}, decide whether every bracket is closed by the same type in the correct order.
"Brackets" closed "in the correct order" means the most recent open must close first. Stack.
Not attempted yet
Implement a first-in first-out queue using only two stacks, with push, pop, peek and empty.
"Using only two stacks": push onto an input stack, and only when the output stack is empty pour everything across. Amortised O(1).
Not attempted yet
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
"Minimum element in constant time": store the running minimum with each pushed value, so popping restores the previous minimum.
Not attempted yet
Evaluate an arithmetic expression given in reverse polish notation. Division truncates toward zero.
"Reverse polish" is postfix: push numbers, pop two per operator.
Not attempted yet
Decode a string like 3[a2[c]] where k[encoded] means repeat encoded k times. Brackets may be nested.
"Nested" brackets with a repeat count: save the outer string and count on a stack at [, restore at ].
Not attempted yet
Asteroids move right if positive and left if negative. When two meet they collide and the smaller explodes. Return the state after all collisions.
A new left-mover can only "collide" with the most recent right-mover still alive: the top of the stack.
Not attempted yet
Simplify an absolute Unix-style file path, resolving '.', '..' and repeated slashes, and return the canonical path.
Directories push and '..' pops the most recent one. "Simplify" a path is a stack of names joined with '/'.
Not attempted yet
Evaluate a string expression with +, -, parentheses and spaces, without using eval.
A "calculator" with "parentheses": on ( push the running result and sign, on ) pop them and combine.
Not attempted yet