Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Check if Stack is Empty or Full in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after pushing elements on the stack?
Consider a stack with a maximum size of 3. We push elements 10, 20, and 30 in order. What will be the printed stack after these operations?
DSA Python
class Stack:
    def __init__(self, max_size):
        self.stack = []
        self.max_size = max_size
    def push(self, item):
        if len(self.stack) < self.max_size:
            self.stack.append(item)
    def __str__(self):
        return ' -> '.join(map(str, self.stack)) + ' -> null'

s = Stack(3)
s.push(10)
s.push(20)
s.push(30)
print(s)
A10 -> 20 -> null
B30 -> 20 -> 10 -> null
C10 -> 20 -> 30 -> null
DStack is empty
Attempts:
2 left
💡 Hint
Remember, push adds elements to the top of the stack, but printing shows from bottom to top in this code.
❓ Predict Output
intermediate
2:00remaining
What does the isEmpty method return after popping all elements?
Given a stack with max size 2, we push 5 and 15, then pop twice. What does isEmpty() return?
DSA Python
class Stack:
    def __init__(self, max_size):
        self.stack = []
        self.max_size = max_size
    def push(self, item):
        if len(self.stack) < self.max_size:
            self.stack.append(item)
    def pop(self):
        if self.stack:
            return self.stack.pop()
        return None
    def isEmpty(self):
        return len(self.stack) == 0

s = Stack(2)
s.push(5)
s.push(15)
s.pop()
s.pop()
print(s.isEmpty())
ATrue
BFalse
CNone
DError
Attempts:
2 left
💡 Hint
Check if the stack has any elements left after popping twice.
❓ Predict Output
advanced
2:00remaining
What error occurs when pushing beyond stack capacity?
What happens when we push an element into a full stack of max size 1?
DSA Python
class Stack:
    def __init__(self, max_size):
        self.stack = []
        self.max_size = max_size
    def push(self, item):
        if len(self.stack) < self.max_size:
            self.stack.append(item)
        else:
            raise OverflowError('Stack is full')

s = Stack(1)
s.push(100)
s.push(200)
AIndexError: list index out of range
BNo error, stack contains [100, 200]
CTypeError: unsupported operand type(s)
DOverflowError: Stack is full
Attempts:
2 left
💡 Hint
Check the condition before appending and what happens if the stack is full.
❓ Predict Output
advanced
2:00remaining
What is the output of isFull after pushing elements?
Given a stack with max size 2, after pushing one element, what does isFull() return?
DSA Python
class Stack:
    def __init__(self, max_size):
        self.stack = []
        self.max_size = max_size
    def push(self, item):
        if len(self.stack) < self.max_size:
            self.stack.append(item)
    def isFull(self):
        return len(self.stack) == self.max_size

s = Stack(2)
s.push(42)
print(s.isFull())
AFalse
BTrue
CError
DNone
Attempts:
2 left
💡 Hint
Compare current stack size with max_size after one push.
🧠 Conceptual
expert
2:00remaining
Which condition correctly checks if a stack is empty?
You want to write a method isEmpty() for a stack implemented as a list named 'stack'. Which condition correctly returns True if the stack is empty and False otherwise?
Areturn self.stack[0] == None
Breturn len(self.stack) == 0
Creturn self.stack == None
Dreturn len(self.stack) > 0
Attempts:
2 left
💡 Hint
Think about how to check if a list has no elements.

Practice

(1/5)
1. What does the isEmpty() function check in a stack?
easy
A. If the stack has exactly one element
B. If the stack is full
C. If the stack has more than one element
D. If the stack has no elements

Solution

  1. Step 1: Understand the purpose of isEmpty()

    The isEmpty() function checks whether the stack contains any elements or not.
  2. Step 2: Identify what empty means for a stack

    An empty stack means it has zero elements, so isEmpty() returns true if no elements are present.
  3. Final Answer:

    If the stack has no elements -> Option D
  4. Quick Check:

    Empty stack means no elements = true [OK]
Hint: Empty stack means zero elements inside [OK]
Common Mistakes:
  • Confusing empty with full
  • Thinking it checks for one element
  • Assuming it checks for capacity
2. Which of the following is the correct syntax to check if a stack named stack is full in Python assuming max size is stored in max_size and current size in top?
easy
A. if top == max_size: print('Stack is full')
B. if top > max_size: print('Stack is full')
C. if top < max_size: print('Stack is full')
D. if top != max_size: print('Stack is full')

Solution

  1. Step 1: Understand stack full condition

    A stack is full when the current top index equals the maximum allowed size.
  2. Step 2: Match condition with syntax

    The correct check is top == max_size to confirm fullness.
  3. Final Answer:

    if top == max_size: print('Stack is full') -> Option A
  4. Quick Check:

    Full stack when top equals max size [OK]
Hint: Full stack means top equals max size [OK]
Common Mistakes:
  • Using greater than instead of equal
  • Using less than for full check
  • Using not equal instead of equal
3. What will be the output of the following code?
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())
medium
A. False\nTrue
B. True\nTrue
C. True\nFalse
D. False\nFalse

Solution

  1. Step 1: Check stack contents after append

    Two elements (10, 20) are added, so length is 2 which equals max_size.
  2. Step 2: Evaluate isFull and isEmpty

    isFull() returns True because length equals max_size; isEmpty() returns False because stack is not empty.
  3. Final Answer:

    True False -> Option C
  4. Quick Check:

    Stack full = True, empty = False [OK]
Hint: Full if length equals max size; empty if length zero [OK]
Common Mistakes:
  • Confusing isFull and isEmpty return values
  • Ignoring stack length after append
  • Assuming stack is empty after append
4. Find the error in the following code that checks if a stack is empty:
class Stack:
    def __init__(self):
        self.stack = []
    def isEmpty(self):
        if self.stack == 0:
            return True
        else:
            return False

s = Stack()
print(s.isEmpty())
medium
A. Missing return statement in isEmpty
B. Comparing list to integer zero causes error
C. Stack initialization is incorrect
D. Print statement syntax is wrong

Solution

  1. Step 1: Analyze the isEmpty condition

    The code compares self.stack == 0, but self.stack is a list, not a number.
  2. Step 2: Identify correct way to check empty list

    To check if list is empty, use len(self.stack) == 0 or simply not self.stack.
  3. Final Answer:

    Comparing list to integer zero causes error -> Option B
  4. Quick Check:

    List cannot be compared to number 0 [OK]
Hint: Check list emptiness with length or not operator [OK]
Common Mistakes:
  • Comparing list directly to 0
  • Forgetting list length check
  • Using wrong comparison operator
5. You have a stack with max size 3. After pushing 3 elements, you pop one element and then check if the stack is full or empty. What will be the correct output of 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())
hard
A. False\nFalse
B. True\nFalse
C. False\nTrue
D. True\nTrue

Solution

  1. Step 1: Track stack size after operations

    Initially push 3 elements, stack size = 3 (full). After pop, size reduces to 2.
  2. Step 2: Evaluate isFull and isEmpty

    Since size is 2 and max_size is 3, isFull() returns False. Stack is not empty, so isEmpty() returns False.
  3. Final Answer:

    False False -> Option A
  4. Quick Check:

    Stack size 2 < max size 3, not empty [OK]
Hint: Pop reduces size; full only if size equals max [OK]
Common Mistakes:
  • Assuming stack is still full after pop
  • Assuming stack is empty after pop
  • Ignoring size changes after pop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard