0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 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.