Stack
easy 14 minA stack is a linear data structure where items are added and removed at the same end, the top.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What does LIFO stand for?
What is the time complexity of push on a stack?
Which operation removes an element from a stack?
Which real-world example best represents a stack?
What causes a stack overflow error?
What causes a stack underflow error?
How do you prevent a stack underflow?
What typically causes a stack overflow in recursive code?
Starting from an empty stack, you run push(1), push(2), pop(), push(3), push(4), pop(). What is on top now?
You push 10, 20, 30, 40 onto a stack and then pop everything. In what order do the values come out?
Pick an answer to see why.
Not quite. LIFO means Last In, First Out: the last element added is the first one removed.
Not quite. Push adds to the top without moving any other element, so it is O(1) (amortised O(1) on a dynamic array).
Not quite. pop() removes and returns the top element. peek() only reads it, and remove() is not a stack operation.
Not quite. With a stack of plates you add to and take from the top only, which is LIFO. A bank queue is FIFO.
Not quite. Overflow happens when you push onto a full stack, or when infinite or very deep recursion exhausts the call stack.
Not quite. Underflow happens when you pop() or peek() an empty stack: there is nothing to remove or read.
Not quite. Checking is_empty() before pop() or peek() stops you from removing an element that is not there. A bigger stack only helps with overflow.
Not quite. Without a reachable base case the function keeps calling itself, pushing frames until the call stack runs out.
Not quite. After push(1), push(2), pop() the stack is [1]. push(3), push(4) gives [1, 3, 4], and pop() removes 4, leaving 3 on top.
Not quite. A stack returns items in the reverse of the order they were pushed, which is why stacks are used to reverse sequences.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What does LIFO stand for, and what does it mean for a stack?
Last In, First Out. The last item you put in is the first one you take out.
Say your answer, then reveal it.
Give a picture that makes LIFO stick.
A stack of pancakes: you always eat the top one first. Or a vertical tower where you can only reach the top and must remove everything above to get lower down.
Say your answer, then reveal it.
Finish the rhyme: "Stack it high, grab from the sky..."
"...last to arrive is first to say goodbye."
Say your answer, then reveal it.
What are the costs of push, pop, peek, is_empty, size and search?
push, pop, peek, is_empty and size are all O(1). Search for a value is O(n). Space is O(n).
Say your answer, then reveal it.
What is a stack overflow, and how do you remember it?
Pushing past the limit: a full fixed-size stack, or infinite or very deep recursion. Overflow = Over capacity or Over recursion, like a glass spilling over the top.
Say your answer, then reveal it.
What is a stack underflow, and how do you remember it?
Popping or peeking from an empty stack. Underflow = Under zero elements, like sucking air from an empty cup.
Say your answer, then reveal it.
What do you check before a push?
Is the stack full? For a fixed size, compare with max capacity. For recursion, is there a valid base case?
Say your answer, then reveal it.
What do you check before a pop or peek?
Is the stack empty? Call is_empty() first, every time, and track size to avoid off-by-one errors.
Say your answer, then reveal it.
Interview sound bite for overflow versus underflow?
Overflow happens when we push too much, past capacity or through infinite recursion. Underflow happens when we pop from empty. Check is_full() before push and is_empty() before pop.
Say your answer, then reveal it.
Which end of a Python list should be the top of a stack, and why?
The end. append and pop() are O(1) there; insert(0, x) and pop(0) shift every element and cost O(n).
Say your answer, then reveal it.
How is a stack related to a queue, a deque and a min stack?
Queue: the opposite order (FIFO). Deque: the generalisation, with access at both ends. Min stack: an extension that also tracks the minimum in O(1). Linked lists are an alternative backing store, and DFS and the call stack are applications.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.