📖 Scenario: Imagine you are building a simple web app that needs a stack to store user actions. The stack should grow automatically when more actions are added than its current size.
🎯 Goal: You will create a dynamic stack using a resizable array in JavaScript. The stack should support push and pop operations and resize the array when it is full.
📋 What You'll Learn
Create an array called stack with initial size 2
Create a variable called top to track the top index of the stack
Write a function push that adds an element to the stack and doubles the array size if full
Write a function pop that removes and returns the top element from the stack
Print the stack array after pushing and popping elements
💡 Why This Matters
🌍 Real World
Dynamic stacks are used in web browsers to manage history, in undo features of apps, and in expression evaluation.
💼 Career
Understanding dynamic data structures like stacks helps in software development roles that involve memory management and algorithm design.
Progress0 / 4 steps
1
Create the initial stack array and top index
Create an array called stack with size 2 filled with null values. Create a variable called top and set it to -1 to represent an empty stack.
DSA Javascript
Hint
Use const stack = [null, null]; to create the array and let top = -1; for the top index.
2
Create a variable for the current capacity of the stack
Create a variable called capacity and set it to 2 to store the current size of the stack array.
DSA Javascript
Hint
Use let capacity = 2; to keep track of the stack size.
3
Write the push function with resizing logic
Write a function called push that takes a parameter value. Inside the function, check if top + 1 equals capacity. If yes, double the capacity, create a new array with the new capacity filled with null, copy all elements from stack to the new array, and assign the new array to stack. Then increase top by 1 and set stack[top] to value.
DSA Javascript
Hint
Check if the stack is full by comparing top + 1 with capacity. If full, double the capacity and copy elements to a new array.
4
Write the pop function and print the stack after operations
Write a function called pop that returns null if top is -1. Otherwise, store the value at stack[top] in a variable, set stack[top] to null, decrease top by 1, and return the stored value. Then, push the values 10, 20, and 30 onto the stack. Pop one value from the stack. Finally, print the stack array.
DSA Javascript
Hint
Return null if the stack is empty. Otherwise, remove the top element and return it. Then test by pushing 10, 20, 30 and popping once.
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 does not allow duplicate elements.
B. It uses less memory than a fixed-size array stack.
C. It always has a fixed capacity to avoid resizing overhead.
D. It can grow or shrink automatically as elements are added or removed.
A fixed-size array stack has a set capacity and cannot grow beyond it, causing overflow if exceeded.
Step 2: Understand dynamic stack advantage
A dynamic stack using a resizable array can increase its capacity automatically when full, allowing more elements without overflow.
Final Answer:
It can grow or shrink automatically as elements are added or removed. -> Option D
Quick Check:
Dynamic resizing = automatic growth/shrink [OK]
Hint: Dynamic stack resizes array automatically when full [OK]
Common Mistakes:
Thinking dynamic stack uses less memory always
Assuming fixed capacity avoids resizing overhead
Believing dynamic stack disallows duplicates
2. Which of the following is the correct way to double the size of the internal array in a dynamic stack implemented in JavaScript?
easy
A. this.array = new Array(this.array.length * 2);
B. this.array = [...this.array, ...new Array(this.array.length)];
C. this.array.length = this.array.length + 1;
D. this.array.push(new Array(this.array.length));
Solution
Step 1: Understand array resizing in JavaScript
JavaScript arrays are dynamic but to simulate resizing, we create a new larger array and copy elements.
Step 2: Analyze options for doubling size
this.array = [...this.array, ...new Array(this.array.length)]; uses spread operator to copy existing elements and adds a new array of same length, effectively doubling size.
Final Answer:
this.array = [...this.array, ...new Array(this.array.length)]; -> Option B
Quick Check:
Spread operator copies + new array doubles size [OK]
Hint: Use spread operator to copy and extend array [OK]
Common Mistakes:
Setting length to length + 1 only increases size by 1, not doubling
Using push with array adds nested array, not size
Creating new array without copying loses data
3. Consider the following JavaScript code for a dynamic stack push operation:
D. Error: Cannot read property 'slice' of undefined
Solution
Step 1: Trace push operations and resizing
Initially array is empty. First push adds 10, array resizes to length 1 (from 0), then 10 stored at index 0. Second push resizes array from length 1 to 2, adds 20 at index 1. Third push resizes from 2 to 4, adds 30 at index 2.
Step 2: Slice array up to top index + 1
top is 2 after three pushes, so slice(0, 3) returns [10, 20, 30]. Extra undefined elements are beyond slice range.
Final Answer:
[10, 20, 30] -> Option C
Quick Check:
Slice array to top+1 = [10, 20, 30] [OK]
Hint: Slice array up to top+1 to get valid stack elements [OK]
Common Mistakes:
Expecting undefined elements in output
Confusing array length with number of elements
Not updating top index correctly
4. The following code attempts to implement pop operation for a dynamic stack but has a bug:
A. Reducing array length directly may remove elements still in use.
B. It does not check if stack is empty before popping.
C. It does not update the top index after popping.
D. It never shrinks the array size.
Solution
Step 1: Understand array shrinking logic
The code shrinks the array length when top is less than quarter of array length.
Step 2: Identify problem with direct length reduction
Reducing array length directly truncates elements beyond new length, which may include valid stack elements if top is not updated properly.
Final Answer:
Reducing array length directly may remove elements still in use. -> Option A
Quick Check:
Direct length cut risks losing valid elements [OK]
Hint: Avoid direct length cut; copy elements to smaller array instead [OK]
Common Mistakes:
Ignoring empty stack check
Not decrementing top index
Assuming array never shrinks
5. You want to implement a dynamic stack that doubles its array size when full and halves it when the number of elements falls below one-fourth of the capacity. Which approach below correctly handles resizing without losing stack elements?
hard
A. Create a new array of the new size and copy existing elements before replacing the old array.
B. Directly set the array length property to new size to resize the array.
C. Use array.push() to add undefined elements to increase size and pop() to reduce size.
D. Do not resize the array; just keep track of top index.
Solution
Step 1: Understand resizing requirements
Doubling and halving array size requires creating new arrays to avoid losing elements.
Step 2: Analyze resizing methods
Create a new array of the new size and copy existing elements before replacing the old array. copies elements to a new array of correct size, preserving stack data safely.
Step 3: Why other options fail
Directly set the array length property to new size to resize the array. truncates or extends array without copying, losing data. Use array.push() to add undefined elements to increase size and pop() to reduce size. misuses push/pop for resizing. Do not resize the array; just keep track of top index. ignores resizing.
Final Answer:
Create a new array of the new size and copy existing elements before replacing the old array. -> Option A
Quick Check:
Copy elements to new array for safe resizing [OK]
Hint: Always copy elements to new array when resizing [OK]
Common Mistakes:
Setting length property directly to resize
Using push/pop to resize array
Ignoring resizing and risking overflow
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.