Check if Stack is Empty or Full in DSA Typescript - Time & Space Complexity
We want to understand how fast we can check if a stack is empty or full.
This helps us know how these checks affect the program speed as the stack grows.
Analyze the time complexity of the following code snippet.
class Stack {
private items: number[] = [];
private maxSize: number;
constructor(size: number) {
this.maxSize = size;
}
isEmpty(): boolean {
return this.items.length === 0;
}
isFull(): boolean {
return this.items.length === this.maxSize;
}
}
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 is a simple property access and does not depend on stack size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter how big the stack is.
Time Complexity: O(1)
This means checking if the stack is empty or full takes the same short time regardless of stack size.
[X] Wrong: "Checking if the stack is full or empty takes longer as the stack grows because it needs to look at all items."
[OK] Correct: The check only looks at the count of items, not each item, so it takes the same time no matter the stack size.
Knowing that these checks are very fast helps you focus on more complex parts of stack operations during interviews.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of checking if it is empty or full change?"
Practice
isEmpty() method check in a stack data structure?Solution
Step 1: Understand the purpose of isEmpty()
TheisEmpty()method is designed to check whether the stack contains any elements or not.Step 2: Interpret the meaning of empty stack
An empty stack means it has zero elements inside, soisEmpty()returns true only when the stack is empty.Final Answer:
If the stack has no elements inside -> Option BQuick Check:
isEmpty() checks for empty stack = A [OK]
- Confusing isEmpty() with isFull()
- Thinking isEmpty() checks for one element
- Assuming isEmpty() checks if stack is full
maxSize and current top index?Solution
Step 1: Understand stack indexing
In a fixed size stack using an array,topstarts at -1 and increments by 1 for each push. The last valid index ismaxSize - 1.Step 2: Check condition for full stack
The stack is full whentopequalsmaxSize - 1, meaning no more space to add elements.Final Answer:
if (top === maxSize - 1) { /* stack is full */ } -> Option AQuick Check:
Full stack when top = maxSize - 1 [OK]
- Using top === maxSize (off by one error)
- Checking top > maxSize (invalid index)
- Using top < maxSize (means not full)
class Stack {
maxSize = 3;
top = -1;
items: number[] = [];
isFull() {
return this.top === this.maxSize - 1;
}
push(item: number) {
if (!this.isFull()) {
this.top++;
this.items[this.top] = item;
}
}
}
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
console.log(stack.items);What will be printed?
Solution
Step 1: Trace push operations with isFull check
Stack maxSize is 3, so top max is 2. Push 10: top=0, items[0]=10; Push 20: top=1, items[1]=20; Push 30: top=2, items[2]=30; Push 40: isFull() returns true (top=2), so push ignored.Step 2: Final stack items array
After all pushes, items array is [10, 20, 30]. The 4th push does not add 40.Final Answer:
[10, 20, 30] -> Option CQuick Check:
Stack full at 3 items, 4th push ignored [OK]
- Assuming push adds all items ignoring full check
- Thinking array grows beyond maxSize
- Confusing top index with array length
class Stack {
top = -1;
isEmpty() {
return this.top === 0;
}
}What is wrong with this
isEmpty() method?Solution
Step 1: Understand top index for empty stack
In stack implementation,topis -1 when stack is empty (no elements pushed yet).Step 2: Identify incorrect condition
The code checkstop === 0, which means one element is present, so it wrongly returns true for non-empty stack.Final Answer:
It should check if top is -1, not 0 -> Option DQuick Check:
Empty stack means top = -1 [OK]
- Checking top === 0 means stack has one element
- Using greater than or less than instead of equality
- Confusing empty with single element condition
canPush() that returns true only if the stack is not full and not empty. Which condition correctly implements this?Solution
Step 1: Define conditions for not empty and not full
Not empty meanstop > -1(at least one element). Not full meanstop < maxSize - 1(space to add more).Step 2: Combine conditions logically
Both conditions must be true, so use logical AND:top > -1 && top < maxSize - 1.Final Answer:
return this.top > -1 && this.top < this.maxSize - 1; -> Option AQuick Check:
Not empty and not full = top > -1 and top < maxSize - 1 [OK]
- Using >= or <= incorrectly allowing empty or full
- Using !== which can fail for boundary values
- Confusing maxSize with maxSize - 1
Ready to prove it?
Pass the test and this pattern moves unknown
