Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

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

Practice

(1/5)
1. What is the main advantage of using a dynamic stack implemented with a resizable array over a fixed-size array stack?
easy
A. It always has a fixed capacity.
B. It uses less memory than a linked list stack.
C. It does not require resizing when full.
D. It can grow or shrink automatically as elements are pushed or popped.

Solution

  1. Step 1: Understand fixed-size array stack limitation

    A fixed-size array stack has a set capacity and cannot grow beyond it, causing overflow if exceeded.
  2. Step 2: Analyze dynamic stack behavior

    A dynamic stack using a resizable array automatically increases or decreases its size when needed, avoiding overflow and wasted space.
  3. Final Answer:

    It can grow or shrink automatically as elements are pushed or popped. -> Option D
  4. Quick Check:

    Dynamic resizing = automatic growth/shrink [OK]
Hint: Dynamic stacks resize arrays automatically when full or empty [OK]
Common Mistakes:
  • Thinking dynamic stack never resizes
  • Confusing dynamic stack with linked list stack
  • Assuming fixed capacity for dynamic stack
2. Which of the following is the correct way to double the size of the internal array in a dynamic stack implemented in Python?
easy
A. self.array.extend([None] * len(self.array))
B. self.array = self.array * 2
C. self.array = [None] * (len(self.array) * 2)
D. self.array.append(self.array)

Solution

  1. Step 1: Understand array resizing in Python list

    To double the size, we add extra None elements equal to current length to the list.
  2. Step 2: Check each option

    self.array.extend([None] * len(self.array)) correctly extends the list by adding None elements equal to current length, doubling size.
  3. Final Answer:

    self.array.extend([None] * len(self.array)) -> Option A
  4. Quick Check:

    Extend with None * current length = double size [OK]
Hint: Use extend with None times current length to double array [OK]
Common Mistakes:
  • Using multiplication on list copies elements, not size
  • Assigning new list loses old elements
  • Appending array inside itself causes nested list
3. Consider this Python code for a dynamic stack push operation:
def push(self, item):
    if self.top == len(self.array):
        self.array.extend([None] * len(self.array))
    self.array[self.top] = item
    self.top += 1

stack = DynamicStack()
stack.push(10)
stack.push(20)
stack.push(30)
print(stack.array[:stack.top])
What will be the printed output?
medium
A. [10, 20, 30]
B. [10, 20, 30, None, None, None]
C. [None, None, None]
D. IndexError

Solution

  1. Step 1: Trace push calls and array state

    Initially array is empty or small. On first push, item 10 is placed at index 0, top becomes 1. Similarly for 20 and 30 at indices 1 and 2.
  2. Step 2: Print array slice up to top

    Printing array[:top] shows only elements at indices 0,1,2 which are [10, 20, 30].
  3. Final Answer:

    [10, 20, 30] -> Option A
  4. Quick Check:

    Print array slice up to top = pushed elements [OK]
Hint: Print only up to top index to see current stack elements [OK]
Common Mistakes:
  • Printing full array including None values
  • Confusing top index with length
  • Expecting error due to resizing
4. Given this buggy pop method for a dynamic stack:
def pop(self):
    if self.top == 0:
        return None
    self.top -= 1
    item = self.array[self.top]
    self.array[self.top] = None
    if self.top < len(self.array) // 4:
        self.array = self.array[:len(self.array) // 2]
    return item
What is the main issue with this code?
medium
A. It does not set popped element to None.
B. It does not check for empty stack before popping.
C. It shrinks the array too early, causing frequent resizing.
D. It doubles the array size instead of shrinking.

Solution

  1. Step 1: Understand shrinking condition

    The array shrinks when top is less than one-fourth of array length, which can cause frequent resizing if elements fluctuate near this threshold.
  2. Step 2: Identify problem with frequent resizing

    Frequent shrinking and expanding wastes time and resources, so better to shrink less aggressively.
  3. Final Answer:

    It shrinks the array too early, causing frequent resizing. -> Option C
  4. Quick Check:

    Shrink threshold too high = frequent resize [OK]
Hint: Shrink array only when usage is very low to avoid thrashing [OK]
Common Mistakes:
  • Ignoring empty stack check (it is present)
  • Confusing shrinking with doubling
  • Not clearing popped element
5. You want to implement a dynamic stack that minimizes memory usage by shrinking the internal array only when the number of elements is less than 1/8 of the array size. Which change should you make to the pop method's shrinking condition?
def pop(self):
    if self.top == 0:
        return None
    self.top -= 1
    item = self.array[self.top]
    self.array[self.top] = None
    if ???:
        self.array = self.array[:len(self.array) // 2]
    return item
hard
A. if self.top == 0
B. if self.top < len(self.array) // 8
C. if self.top <= len(self.array) // 2
D. if self.top < len(self.array) // 4

Solution

  1. Step 1: Understand shrinking threshold

    To shrink only when usage is less than 1/8, the condition must check if top is less than array length divided by 8.
  2. Step 2: Match condition to code

    if self.top < len(self.array) // 8 correctly uses 'if self.top < len(self.array) // 8' to trigger shrinking at 1/8 usage.
  3. Final Answer:

    if self.top < len(self.array) // 8 -> Option B
  4. Quick Check:

    Shrink when usage < 1/8 = condition in if self.top < len(self.array) // 8 [OK]
Hint: Use 1/8 threshold to shrink less often and save memory [OK]
Common Mistakes:
  • Using 1/4 threshold shrinks too early
  • Using equality instead of less than
  • Shrinking when stack is empty only
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