All puzzles › Stacks and Queues
Stacks and Queues Practice Problems
30 puzzles (9 easy, 16 medium, 5 hard), 5 free, in Python, JavaScript and C++.
A stack gives you back the most recent thing you put in; a queue gives you the oldest. That is the whole difference, and choosing between them decides the order your program does its work. Stacks handle anything nested or anything that must be undone in reverse. Queues handle anything that must be dealt with in the order it arrived.
How to spot a stacks and queues problem
Use a stack when each new item has to be compared with the latest unfinished one: brackets, nested expressions, the next larger value to the right. Use a queue when fairness or distance matters: first come first served, or exploring everything one step away before anything two steps away.
New to it? Read the lesson first: Stacks.
The puzzles, easiest first
- Finish the Model's Sentence (Easy, free): A language model's JSON often arrives cut off, with brackets still open.
- Valid Parentheses (Easy, free): Given a string of brackets ( ) [ ] { }, return true if they all close in the right order.
- Deepest Nesting (Easy): Given a string of parentheses, return how deeply they nest.
- Moving Average (Easy): Given a stream of readings and a window size, return the average of the most recent readings after each one, rounded to 2 decimals.
- Only the Last Few (Easy): Given a stream of timestamps and a cap, return the ones you keep, oldest first.
- Pause on Top of Play (Easy): Game menus are a stack: the pause menu sits on top of the game, and closing it returns you to exactly the game underneath.
- Recent Pings (Easy): Given a list of ping times in milliseconds, oldest first, and the current time, return how many pings landed in the last 3000 milliseconds, counting exactly 3000 ago.
- Reverse With a Stack (Easy): Given a list, return it reversed using a stack.
- What's Left on the Stack (Easy): Given a list of commands, return what's left on the stack, bottom first.
- Which Shares Did You Sell? (Medium, free): You bought shares at different prices over time, and now you sell some.
- Backspace Compare (Medium): Given two strings where "#" means backspace, return true if they end up as the same text.
- Balanced Tags (Medium): Given a list of tags like "div" and "/div", return true if they're properly nested and every tag gets closed.
- Cancel Neighbours (Medium): Given a string, keep removing any two identical letters that sit side by side until there are none left, and return what's left.
- Circular Queue (Medium): Given a list of values and a buffer size, return what's in the buffer at the end, oldest first.
- Ctrl+Z in the Level Editor (Medium): A level editor gets commands: "place NAME" puts down a tile, "undo" takes back the last placement, and "redo" puts back the last thing undone.
- Days Until Warmer (Medium): Given daily temperatures, return for each day how many days until a warmer one, or 0 if none comes.
- Decode a Repeat (Medium): Given a compressed string like "3[a]2[bc]", expand it to "aaabcbc".
- Don't Eat Yourself (Medium): A snake starts at row 0, column 0 on a board of the given width and height.
- Next Greater Element (Medium): Given an array, return for each value the next bigger value to its right, or -1 if there isn't one.
- Queue From Two Stacks (Medium): Given values pushed in order onto a stack, return the order they'd come out if it worked like a queue.
- Scoreboard Ops (Medium): Given a list of scoreboard commands, return the total.
- Simplify a Path (Medium): Given a messy file path like "/a/./b/../../c/", return the clean version, "/c".
- Time to Buy (Medium): Given how many tickets each person in line wants and a position k, return how many seconds until person k is done.
- Too Deep (Medium): Given a list of calls, where a positive number is a call that adds a stack frame of that many bytes and 0 is a return from the latest call, plus a size limit, return the index of the first call that overflows the stack, or -1 if none does.
- Track the Minimum (Medium): Given a list of stack commands, where a number is a push and "pop" removes the top, return the smallest value left on the stack at the end, or null if it's empty.
- A Second Chance (Hard, free): Exact least-recently-used eviction is too expensive for an operating system, so most use CLOCK.
- Never Go Down (Hard, free): Sometimes all you know about a curve is that it must never go down: a higher model score should not mean a lower probability.
- Asteroid Collision (Hard): Given a row of asteroids, where positive ones fly right and negative ones fly left, return what's left after every crash.
- Sort Using a Stack (Hard): Given a stack, sort it using only one extra stack, and return it from smallest at the bottom to largest on top.
- Stock Span (Hard): Given daily stock prices, return each day's span: how many days in a row, ending today, the price was less than or equal to today's.
Practice another pattern
Or browse by subject
Browse all 676 puzzles