0
0
DSA Pythonprogramming~20 mins

Stack Implementation Using Array 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 of the stack after these operations?
Consider a stack implemented using an array with a maximum size of 5. The following operations are performed in order:

push(10), push(20), pop(), push(30), push(40), pop(), push(50)

What is the printed state of the stack (top to bottom) after these operations?
DSA Python
class Stack:
    def __init__(self, max_size=5):
        self.stack = [None] * max_size
        self.top = -1
        self.max_size = max_size

    def push(self, value):
        if self.top == self.max_size - 1:
            return "Stack Overflow"
        self.top += 1
        self.stack[self.top] = value

    def pop(self):
        if self.top == -1:
            return "Stack Underflow"
        val = self.stack[self.top]
        self.top -= 1
        return val

    def print_stack(self):
        if self.top == -1:
            print("Stack is empty")
        else:
            for i in range(self.top, -1, -1):
                print(self.stack[i], end=" -> " if i != 0 else " -> null\n")

s = Stack()
s.push(10)
s.push(20)
s.pop()
s.push(30)
s.push(40)
s.pop()
s.push(50)
s.print_stack()
A30 -> 10 -> null
B40 -> 30 -> 10 -> null
C50 -> 40 -> 30 -> 10 -> null
D50 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds on top.
Predict Output
intermediate
1:30remaining
What error does this stack operation cause?
Given a stack with max size 3, what error will occur after these operations?

push(1), push(2), push(3), push(4)
DSA Python
class Stack:
    def __init__(self, max_size=3):
        self.stack = [None] * max_size
        self.top = -1
        self.max_size = max_size

    def push(self, value):
        if self.top == self.max_size - 1:
            return "Stack Overflow"
        self.top += 1
        self.stack[self.top] = value

s = Stack()
s.push(1)
s.push(2)
s.push(3)
result = s.push(4)
print(result)
AStack Overflow
BStack Underflow
CNone
DIndexError
Attempts:
2 left
💡 Hint
Check what happens when you try to push beyond max size.
🔧 Debug
advanced
1:30remaining
Why does this pop method cause an error?
This pop method is part of a stack implemented using an array. What error will it cause when popping from an empty stack?
DSA Python
def pop(self):
    val = self.stack[self.top]
    self.top -= 1
    return val
AAttributeError: 'Stack' object has no attribute 'top'
BIndexError: list index out of range
CTypeError: unsupported operand type(s) for -=: 'NoneType' and 'int'
DNo error, returns None
Attempts:
2 left
💡 Hint
What happens if top is -1 and you try to access stack[top]?
Predict Output
advanced
2:00remaining
What is the final stack content after these operations?
A stack with max size 4 is empty initially. The following operations are done:

push(5), push(10), pop(), push(15), push(20), push(25), pop(), pop()

Print the stack from top to bottom.
DSA Python
class Stack:
    def __init__(self, max_size=4):
        self.stack = [None] * max_size
        self.top = -1
        self.max_size = max_size

    def push(self, value):
        if self.top == self.max_size - 1:
            return "Stack Overflow"
        self.top += 1
        self.stack[self.top] = value

    def pop(self):
        if self.top == -1:
            return "Stack Underflow"
        val = self.stack[self.top]
        self.top -= 1
        return val

    def print_stack(self):
        if self.top == -1:
            print("Stack is empty")
        else:
            for i in range(self.top, -1, -1):
                print(self.stack[i], end=" -> " if i != 0 else " -> null\n")

s = Stack()
s.push(5)
s.push(10)
s.pop()
s.push(15)
s.push(20)
s.push(25)
s.pop()
s.pop()
s.print_stack()
A10 -> 15 -> 5 -> null
B20 -> 15 -> 5 -> null
C15 -> 5 -> null
D25 -> 20 -> 15 -> 5 -> null
Attempts:
2 left
💡 Hint
Track each push and pop carefully.
🧠 Conceptual
expert
1:00remaining
What is the time complexity of push and pop operations in a stack implemented using an array?
Consider a stack implemented using a fixed-size array. What is the time complexity of the push and pop operations?
ABoth push and pop are O(1) time complexity
BPush is O(n), pop is O(1)
CPush is O(1), pop is O(n)
DBoth push and pop are O(n) time complexity
Attempts:
2 left
💡 Hint
Think about how many steps are needed to add or remove one element at the top.