Challenge - 5 Problems
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that pop removes the last pushed element and resizing happens when size is one-fourth of capacity.
✗ Incorrect
After pushing 10, 20, 30, the stack is 10 -> 20 -> 30 -> null. Popping removes 30. Then pushing 40 adds it on top. So stack is 10 -> 20 -> 40 -> null.
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Check when the array resizes down after popping elements.
✗ Incorrect
Initially capacity is 2. After pushing 4 elements, capacity grows to 4. After popping 3 elements, size is 1 which triggers resize down to capacity 2.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check the loop range compared to size of array.
✗ Incorrect
Loop runs from 0 to size inclusive, but valid indices are 0 to size-1. This causes IndexError.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about memory usage when many elements are removed.
✗ Incorrect
Reducing array size when stack shrinks saves memory by not holding unused space.
❓ Predict Output
expert3: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)
Attempts:
2 left
💡 Hint
Track pushes and pops carefully and remember pop removes last pushed element.
✗ Incorrect
Pushed 1 to 8, then popped 5 times removing 8,7,6,5,4. Left with 1,2,3. Then pushed 100 and 200 on top.