Check if Stack is Empty or Full in DSA Javascript - Time & Space Complexity
We want to understand how fast we can check if a stack is empty or full.
This helps us know how much time these checks take as the stack size changes.
Analyze the time complexity of the following code snippet.
class Stack {
constructor(size) {
this.size = size;
this.items = [];
}
isEmpty() {
return this.items.length === 0;
}
isFull() {
return this.items.length === this.size;
}
}
This code checks if the stack has no items or if it reached its maximum size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the length property of the array.
- How many times: Once per call to
isEmptyorisFull.
Checking the length does not depend on how many items are in the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter the stack size.
Time Complexity: O(1)
This means checking if the stack is empty or full takes the same small amount of time regardless of stack size.
[X] Wrong: "Checking if the stack is full or empty takes longer as the stack grows because it looks at all items."
[OK] Correct: The code only checks the length property, which is stored and accessed instantly, not by scanning all items.
Knowing that simple checks like empty or full run instantly shows you understand efficient data structure operations.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of checking if it is full or empty change?"
Practice
empty?Solution
Step 1: Understand the stack empty condition
A stack is empty when it contains no elements inside it.Step 2: Compare with other options
Options about full stack or having elements do not describe empty stack.Final Answer:
There are no elements in the stack. -> Option CQuick Check:
Empty stack means no elements [OK]
- Confusing empty with full
- Thinking empty means one element
- Mixing empty with partially filled
stack is empty?Solution
Step 1: Check correct property for array length
JavaScript arrays uselengthproperty to get number of elements.Step 2: Verify condition for empty stack
Empty means length is zero, sostack.length === 0is correct.Final Answer:
if (stack.length === 0) { /* empty */ } -> Option AQuick Check:
Use length property to check empty [OK]
- Using null instead of length
- Using size property (not for arrays)
- Checking length > 0 for empty
const stack = []; const maxSize = 3; stack.push(10); stack.push(20); stack.push(30); const isFull = stack.length === maxSize; const isEmpty = stack.length === 0; console.log(isFull, isEmpty);What will be printed?
Solution
Step 1: Trace stack push operations
Three elements (10, 20, 30) are pushed, so stack length is 3.Step 2: Evaluate isFull and isEmpty
isFull = stack.length === maxSize -> 3 === 3 -> true; isEmpty = stack.length === 0 -> 3 === 0 -> false.Final Answer:
true false -> Option DQuick Check:
Stack full = true, empty = false [OK]
- Confusing isFull and isEmpty
- Forgetting to update length after push
- Assuming stack is empty after pushes
const stack = [1, 2, 3];
const maxSize = 3;
function isStackFull() {
if (stack.length = maxSize) {
return true;
} else {
return false;
}
}Solution
Step 1: Identify the if condition mistake
The condition uses single equals=which assigns value instead of comparing.Step 2: Correct comparison operator
It should use===to compare stack length with maxSize.Final Answer:
Using assignment (=) instead of comparison (===) in if condition. -> Option AQuick Check:
Use === for comparison, not = [OK]
- Using = instead of ===
- Confusing maxSize with stack length
- Returning wrong type from function
Solution
Step 1: Check conditions for full and empty stack
Full means length equals max size (5), empty means length zero.Step 2: Use else for partial stack
If neither full nor empty, stack is partially filled.Final Answer:
if (stack.length === 5) { console.log('Full'); } else if (stack.length === 0) { console.log('Empty'); } else { console.log('Partial'); } -> Option BQuick Check:
Check length === maxSize for full, === 0 for empty [OK]
- Using wrong comparison operators
- Swapping full and empty conditions
- Checking length > maxSize or < 0
Ready to prove it?
Pass the test and this pattern moves unknown
