Challenge - 5 Problems
Dynamic Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push and Pop Operations on Dynamic Stack
What is the printed state of the stack after the following operations?
Push 10, Push 20, Push 30, Pop, Push 40
Push 10, Push 20, Push 30, Pop, Push 40
DSA Java
class DynamicStack { private int[] arr; private int top; private int capacity; public DynamicStack() { capacity = 2; arr = new int[capacity]; top = -1; } public void push(int x) { if (top == capacity - 1) { int[] newArr = new int[capacity * 2]; for (int i = 0; i < capacity; i++) { newArr[i] = arr[i]; } arr = newArr; capacity *= 2; } arr[++top] = x; } public int pop() { if (top == -1) return -1; return arr[top--]; } public String printStack() { if (top == -1) return "Stack is empty"; StringBuilder sb = new StringBuilder(); for (int i = 0; i <= top; i++) { sb.append(arr[i]); if (i != top) sb.append(" -> "); } sb.append(" -> null"); return sb.toString(); } } public class Main { public static void main(String[] args) { DynamicStack stack = new DynamicStack(); stack.push(10); stack.push(20); stack.push(30); stack.pop(); stack.push(40); System.out.println(stack.printStack()); } }
Attempts:
2 left
💡 Hint
Remember that pop removes the top element before the next push.
✗ Incorrect
Initially, stack has 10, then 20, then 30. Pop removes 30. Then push 40 adds it on top. So stack is 10 -> 20 -> 40 -> null.
❓ Predict Output
intermediateCapacity Changes in Dynamic Stack
What is the capacity of the internal array after pushing 5 elements into the stack starting from empty?
DSA Java
class DynamicStack { private int[] arr; private int top; private int capacity; public DynamicStack() { capacity = 2; arr = new int[capacity]; top = -1; } public void push(int x) { if (top == capacity - 1) { int[] newArr = new int[capacity * 2]; for (int i = 0; i < capacity; i++) { newArr[i] = arr[i]; } arr = newArr; capacity *= 2; } arr[++top] = x; } public int getCapacity() { return capacity; } } public class Main { public static void main(String[] args) { DynamicStack stack = new DynamicStack(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); System.out.println(stack.getCapacity()); } }
Attempts:
2 left
💡 Hint
Capacity doubles when full before pushing new element.
✗ Incorrect
Initial capacity is 2. After pushing 2 elements, capacity doubles to 4. Pushing the 3rd element triggers resize to 4. Pushing 4th fills capacity. Pushing 5th triggers resize to 8.
🔧 Debug
advancedIdentify the Error in Pop Method
What error will occur when calling pop on an empty stack with this code?
DSA Java
public int pop() { if (top == -1) return -1; return arr[top--]; }
Attempts:
2 left
💡 Hint
Check the condition for empty stack before accessing array.
✗ Incorrect
The method returns -1 if stack is empty (top == -1), so no exception occurs.
❓ Predict Output
advancedStack Content After Multiple Resizes
What is the printed stack after pushing elements 1 to 10 in order?
DSA Java
class DynamicStack { private int[] arr; private int top; private int capacity; public DynamicStack() { capacity = 2; arr = new int[capacity]; top = -1; } public void push(int x) { if (top == capacity - 1) { int[] newArr = new int[capacity * 2]; for (int i = 0; i < capacity; i++) { newArr[i] = arr[i]; } arr = newArr; capacity *= 2; } arr[++top] = x; } public String printStack() { if (top == -1) return "Stack is empty"; StringBuilder sb = new StringBuilder(); for (int i = 0; i <= top; i++) { sb.append(arr[i]); if (i != top) sb.append(" -> "); } sb.append(" -> null"); return sb.toString(); } } public class Main { public static void main(String[] args) { DynamicStack stack = new DynamicStack(); for (int i = 1; i <= 10; i++) { stack.push(i); } System.out.println(stack.printStack()); } }
Attempts:
2 left
💡 Hint
Push adds elements on top, printStack prints from bottom to top.
✗ Incorrect
Elements are pushed in order 1 to 10. printStack prints from bottom (index 0) to top (index top). So output is 1 -> 2 -> ... -> 10 -> null.
🧠 Conceptual
expertWhy Use Dynamic Array for Stack Instead of Fixed Array?
Which is the main advantage of using a dynamic array for implementing a stack compared to a fixed-size array?
Attempts:
2 left
💡 Hint
Think about what happens when you push more elements than the initial size.
✗ Incorrect
Dynamic arrays resize automatically when full, allowing the stack to grow as needed without fixed limits.
