0
0
DSA Pythonprogramming~20 mins

Dynamic Stack Using Resizable Array in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Push and Pop Operations on Dynamic Stack
What is the printed state of the stack after the following operations?
DSA Python
class DynamicStack:
    def __init__(self):
        self.array = [None] * 2
        self.size = 0

    def push(self, val):
        if self.size == len(self.array):
            self.resize(2 * len(self.array))
        self.array[self.size] = val
        self.size += 1

    def pop(self):
        if self.size == 0:
            return None
        self.size -= 1
        val = self.array[self.size]
        self.array[self.size] = None
        if self.size > 0 and self.size == len(self.array) // 4:
            self.resize(len(self.array) // 2)
        return val

    def resize(self, capacity):
        new_array = [None] * capacity
        for i in range(self.size):
            new_array[i] = self.array[i]
        self.array = new_array

    def __str__(self):
        return ' -> '.join(str(self.array[i]) for i in range(self.size)) + ' -> null'

stack = DynamicStack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.pop()
stack.push(40)
print(stack)
A10 -> 20 -> null
B10 -> 20 -> 30 -> 40 -> null
C10 -> 20 -> 40 -> null
D20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember that pop removes the last pushed element and resizing happens when size is one-fourth of capacity.
Predict Output
intermediate
2:00remaining
Capacity of Array After Pushes and Pops
What is the capacity of the internal array after these operations?
DSA Python
stack = DynamicStack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.pop()
stack.pop()
stack.pop()
print(len(stack.array))
A2
B4
C1
D8
Attempts:
2 left
💡 Hint
Check when the array resizes down after popping elements.
🔧 Debug
advanced
2:00remaining
Identify the Error in Resize Method
What error will occur when running this resize method in the DynamicStack class?
DSA Python
def resize(self, capacity):
    new_array = [None] * capacity
    for i in range(self.size + 1):
        new_array[i] = self.array[i]
    self.array = new_array
AIndexError because loop goes out of bounds
BTypeError because new_array is not a list
CNo error, works correctly
DValueError because capacity is negative
Attempts:
2 left
💡 Hint
Check the loop range compared to size of array.
🧠 Conceptual
advanced
1:30remaining
Why Does Dynamic Stack Resize Down?
Why does the dynamic stack reduce its array size when the number of elements is one-fourth of the capacity?
ATo prevent stack overflow errors
BTo increase speed of push operations
CTo keep the array size always even
DTo save memory and avoid wasting space when stack shrinks
Attempts:
2 left
💡 Hint
Think about memory usage when many elements are removed.
Predict Output
expert
3:00remaining
Final Stack State After Complex Operations
What is the printed stack after these operations?
DSA Python
stack = DynamicStack()
for i in range(1, 9):
    stack.push(i)
for _ in range(5):
    stack.pop()
stack.push(100)
stack.push(200)
print(stack)
A100 -> 200 -> null
B1 -> 2 -> 3 -> 100 -> 200 -> null
C1 -> 2 -> 3 -> 4 -> 5 -> 100 -> 200 -> null
D4 -> 5 -> 6 -> 7 -> 8 -> 100 -> 200 -> null
Attempts:
2 left
💡 Hint
Track pushes and pops carefully and remember pop removes last pushed element.