Stack operations (push, pop, peek) in Data Structures Theory - Time & Space Complexity
When working with stacks, it is important to know how fast basic actions like adding or removing items happen.
We want to understand how the time to do these actions changes as the stack grows.
Analyze the time complexity of the following stack operations.
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
This code shows how to add, remove, and look at the top item of a stack using a list.
Each operation works on the end of the list without loops or recursion.
- Primary operation: Adding or removing one item at the end of the list.
- How many times: Each operation happens once per call, no repeated steps inside.
Each push, pop, or peek takes about the same time no matter how many items are in the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even as the stack grows larger.
Time Complexity: O(1)
This means each stack operation takes the same short time no matter how many items are in the stack.
[X] Wrong: "Pop or push takes longer when the stack is very big."
[OK] Correct: These operations only touch the top item, so their time does not grow with stack size.
Knowing that stack operations are quick and constant time helps you explain why stacks are useful in many programs.
"What if the stack was implemented using a linked list instead of a list? How would the time complexity change?"
Practice
pop operation do in a stack?Solution
Step 1: Understand the pop operation
The pop operation removes the last item added to the stack, which is the top item.Step 2: Confirm the behavior of pop
It returns this top item and removes it from the stack, following last-in, first-out order.Final Answer:
Removes and returns the top item from the stack -> Option CQuick Check:
pop = remove top item [OK]
- Thinking pop adds an item
- Confusing pop with peek
- Assuming pop removes bottom item
5 to a stack named s using the push operation?Solution
Step 1: Identify the push syntax
Push adds an item to the top of the stack, usually called withpush(item).Step 2: Match the correct method call
Onlys.push(5)correctly adds 5 to the stack.Final Answer:
s.push(5) -> Option BQuick Check:
push = add item [OK]
- Using pop instead of push
- Using peek to add items
- Using add which is not standard for stacks
s.push(10)
s.push(20)
s.pop()
s.push(30)
top = s.peek()
What is the value of
top after these operations?Solution
Step 1: Trace the stack changes step-by-step
Afterpush(10), stack = [10]. Afterpush(20), stack = [10, 20]. Afterpop(), top 20 is removed, stack = [10]. Afterpush(30), stack = [10, 30].Step 2: Determine the top item with peek
Peek returns the top item without removing it, sotop = 30.Final Answer:
30 -> Option AQuick Check:
Last pushed after pop is 30 [OK]
- Forgetting pop removes top
- Assuming peek removes item
- Mixing order of operations
stack = [] stack.push(1) stack.push(2) item = stack.pop()
What is the error in this code?
Solution
Step 1: Check stack initialization
The stack is initialized as an empty list, which is correct in Python.Step 2: Verify method usage on list
Python lists useappend()to add items, notpush(). Usingpush()causes an error.Final Answer:
push() is not a method of list in Python -> Option DQuick Check:
Python list uses append(), not push() [OK]
- Assuming push() works on Python lists
- Confusing pop() behavior
- Thinking stack initialization is wrong
[1, 2, 3] using a stack. Which sequence of stack operations correctly reverses the list?Solution
Step 1: Understand stack reversal logic
Pushing items in original order stores them with last item on top. Popping all items removes them in reverse order.Step 2: Apply push then pop to reverse list
Push 1, then 2, then 3. Popping returns 3, 2, 1, which is the reversed list.Final Answer:
Push all items in order, then pop all items to get reversed list -> Option AQuick Check:
Push then pop reverses order [OK]
- Pushing in reverse order first
- Using peek to reverse
- Popping before pushing
Ready to prove it?
Pass the test and this pattern moves unknown
