Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Stack Implementation Using Array 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 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.

Practice

(1/5)
1. What is the main purpose of using an array to implement a stack?
easy
A. To allow random access to elements
B. To store elements in a first-in, first-out (FIFO) order
C. To sort elements automatically
D. To store elements in a last-in, first-out (LIFO) order

Solution

  1. Step 1: Understand stack behavior

    A stack stores elements so that the last element added is the first one removed, called LIFO.
  2. Step 2: Role of array in stack

    An array helps store elements in order and access the last added element easily for push/pop operations.
  3. Final Answer:

    To store elements in a last-in, first-out (LIFO) order -> Option D
  4. Quick Check:

    Stack = LIFO [OK]
Hint: Stack means last-in, first-out order [OK]
Common Mistakes:
  • Confusing stack with queue (FIFO)
  • Thinking array sorts elements automatically
  • Assuming random access is main stack feature
2. Which of the following is the correct way to initialize an empty stack using an array in Python?
easy
A. stack = []
B. stack = {}
C. stack = ()
D. stack = null

Solution

  1. Step 1: Identify Python data types for stack

    An array-like structure for stack is a list, initialized with square brackets [].
  2. Step 2: Check each option

    stack = [] uses [], which is a list. stack = {} is a dictionary, () is a tuple, null (no data).
  3. Final Answer:

    stack = [] -> Option A
  4. Quick Check:

    Empty list = empty stack [OK]
Hint: Use [] to create empty list for stack [OK]
Common Mistakes:
  • Using {} which creates a dictionary
  • Using () which creates a tuple
  • Assigning null instead of a list
3. What will be the output after executing the following code?
stack = []
stack.append(10)
stack.append(20)
stack.pop()
print(stack)
medium
A. [10, 20]
B. [20]
C. [10]
D. []

Solution

  1. Step 1: Trace push operations

    stack.append(10) adds 10, stack becomes [10]. Then stack.append(20) adds 20, stack becomes [10, 20].
  2. Step 2: Trace pop operation

    stack.pop() removes last element 20, stack becomes [10].
  3. Final Answer:

    [10] -> Option C
  4. Quick Check:

    Pop removes last item, stack left with [10] [OK]
Hint: Pop removes last pushed element [OK]
Common Mistakes:
  • Thinking pop removes first element
  • Confusing append with insert
  • Expecting stack to be empty after pop
4. Identify the error in the following stack implementation code:
stack = []
stack.push(5)
stack.append(10)
print(stack)
medium
A. 'push' method does not exist for list
B. Missing initialization of stack
C. Cannot append to list
D. print statement syntax error

Solution

  1. Step 1: Check list methods

    Python lists use append() to add elements, not push().
  2. Step 2: Identify error

    stack.push(5) causes AttributeError because 'push' is not a list method.
  3. Final Answer:

    'push' method does not exist for list -> Option A
  4. Quick Check:

    Use append(), not push() for list [OK]
Hint: Use append() to add elements to list [OK]
Common Mistakes:
  • Using push() instead of append()
  • Assuming push() is valid for Python lists
  • Ignoring error messages
5. Given a stack implemented using an array, how can you check if the stack is empty before popping an element to avoid errors?
hard
A. Check if stack is null
B. Check if stack's length is zero using len(stack) == 0
C. Try popping and catch exceptions if stack is empty
D. Use stack[0] == null to check emptiness

Solution

  1. Step 1: Understand stack emptiness check

    To avoid errors, check if stack has no elements by verifying its length is zero.
  2. Step 2: Evaluate options

    Check if stack's length is zero using len(stack) == 0 correctly checks length with len(stack) == 0. Try popping and catch exceptions if stack is empty is valid but less efficient. Check if stack is null checks null which is incorrect. Use stack[0] == null to check emptiness checks first element which may cause error if empty.
  3. Final Answer:

    Check if stack's length is zero using len(stack) == 0 -> Option B
  4. Quick Check:

    Empty stack means length zero [OK]
Hint: Use len(stack) == 0 to check empty stack [OK]
Common Mistakes:
  • Checking stack is null instead of empty
  • Accessing stack[0] without checking length
  • Relying only on try-except without condition
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