What if your program crashes just because it tried to add to a full stack or remove from an empty one?
Why Check if Stack is Empty or Full in DSA Python?
Imagine you have a stack of plates in your kitchen. You want to add a new plate or take one out, but you need to know if the stack is already empty or too full to add more.
Without checking, you might try to take a plate from an empty stack and drop nothing, or add a plate to a full stack and make a mess. Manually keeping track is confusing and leads to mistakes.
By having simple checks to see if the stack is empty or full, you avoid errors and keep your stack safe and organized. These checks tell you exactly when you can add or remove plates.
if len(stack) == 0: print('Stack is empty') else: stack.pop()
if stack.is_empty(): print('Stack is empty') else: stack.pop()
This lets you safely manage your stack without errors, making your program reliable and easy to use.
When using a browser's back button, the browser checks if the history stack is empty before going back, preventing errors or crashes.
Manual tracking of stack state is error-prone.
Checking if stack is empty or full prevents mistakes.
These checks keep your program safe and reliable.
Practice
isEmpty() function check in a stack?Solution
Step 1: Understand the purpose of
TheisEmpty()isEmpty()function checks whether the stack contains any elements or not.Step 2: Identify what empty means for a stack
An empty stack means it has zero elements, soisEmpty()returns true if no elements are present.Final Answer:
If the stack has no elements -> Option DQuick Check:
Empty stack means no elements = true [OK]
- Confusing empty with full
- Thinking it checks for one element
- Assuming it checks for capacity
stack is full in Python assuming max size is stored in max_size and current size in top?Solution
Step 1: Understand stack full condition
A stack is full when the current top index equals the maximum allowed size.Step 2: Match condition with syntax
The correct check istop == max_sizeto confirm fullness.Final Answer:
if top == max_size: print('Stack is full') -> Option AQuick Check:
Full stack when top equals max size [OK]
- Using greater than instead of equal
- Using less than for full check
- Using not equal instead of equal
class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
def isFull(self):
return len(self.stack) == self.max_size
def isEmpty(self):
return len(self.stack) == 0
s = Stack(2)
s.stack.append(10)
s.stack.append(20)
print(s.isFull())
print(s.isEmpty())Solution
Step 1: Check stack contents after append
Two elements (10, 20) are added, so length is 2 which equals max_size.Step 2: Evaluate isFull and isEmpty
isFull()returns True because length equals max_size;isEmpty()returns False because stack is not empty.Final Answer:
True False -> Option CQuick Check:
Stack full = True, empty = False [OK]
- Confusing isFull and isEmpty return values
- Ignoring stack length after append
- Assuming stack is empty after append
class Stack:
def __init__(self):
self.stack = []
def isEmpty(self):
if self.stack == 0:
return True
else:
return False
s = Stack()
print(s.isEmpty())Solution
Step 1: Analyze the isEmpty condition
The code comparesself.stack == 0, butself.stackis a list, not a number.Step 2: Identify correct way to check empty list
To check if list is empty, uselen(self.stack) == 0or simplynot self.stack.Final Answer:
Comparing list to integer zero causes error -> Option BQuick Check:
List cannot be compared to number 0 [OK]
- Comparing list directly to 0
- Forgetting list length check
- Using wrong comparison operator
isFull() and isEmpty() methods?class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
def push(self, val):
if len(self.stack) < self.max_size:
self.stack.append(val)
def pop(self):
if not self.isEmpty():
return self.stack.pop()
def isFull(self):
return len(self.stack) == self.max_size
def isEmpty(self):
return len(self.stack) == 0
s = Stack(3)
s.push(1)
s.push(2)
s.push(3)
s.pop()
print(s.isFull())
print(s.isEmpty())Solution
Step 1: Track stack size after operations
Initially push 3 elements, stack size = 3 (full). After pop, size reduces to 2.Step 2: Evaluate isFull and isEmpty
Since size is 2 and max_size is 3,isFull()returns False. Stack is not empty, soisEmpty()returns False.Final Answer:
False False -> Option AQuick Check:
Stack size 2 < max size 3, not empty [OK]
- Assuming stack is still full after pop
- Assuming stack is empty after pop
- Ignoring size changes after pop
Ready to prove it?
Pass the test and this pattern moves unknown
