Check if Stack is Empty or Full in DSA C - Time & Space Complexity
Checking if a stack is empty or full is a common operation in stack management.
We want to know how fast these checks run as the stack size changes.
Analyze the time complexity of the following code snippet.
// Check if stack is empty
int isEmpty(int top) {
return top == -1;
}
// Check if stack is full
int isFull(int top, int capacity) {
return top == capacity - 1;
}
This code checks if the stack is empty or full by comparing the top index with limits.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple comparison operations.
- How many times: Each check runs once per call, no loops or recursion.
These checks do not depend on the stack size; they always do the same small work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 comparison |
| 100 | 1 comparison |
| 1000 | 1 comparison |
Pattern observation: The number of operations stays the same no matter how big the stack is.
Time Complexity: O(1)
This means the check runs in constant time, always very fast regardless of stack size.
[X] Wrong: "Checking if stack is full or empty takes longer as the stack grows."
[OK] Correct: These checks only compare two numbers, so they take the same time no matter the stack size.
Understanding constant time checks like these helps you write efficient stack operations and shows you know how to reason about simple but important code.
"What if we changed the stack to use a linked list instead of an array? How would the time complexity of checking if it is empty or full change?"
Practice
isEmpty() typically check in a stack data structure?Solution
Step 1: Understand the purpose of
TheisEmpty()isEmpty()function checks whether the stack contains any elements or not.Step 2: Identify what 'empty' means for a stack
A stack is empty when it has zero elements, meaning no data is stored inside.Final Answer:
If the stack has no elements -> Option CQuick Check:
Empty stack means zero elements = true [OK]
- Confusing empty with full
- Thinking empty means one element
- Mixing up stack size with capacity
top is the index of the top element and MAX is the stack capacity?Solution
Step 1: Understand stack indexing
Stack indices start at 0, so the last valid index isMAX - 1.Step 2: Check condition for full stack
The stack is full whentoppoints to the last index, which isMAX - 1.Final Answer:
if (top == MAX - 1) -> Option AQuick Check:
Top equals max index means full [OK]
- Using top == MAX (off by one error)
- Using top > MAX (invalid index)
- Using top < MAX - 1 (means not full)
MAX = 3:
int stack[MAX];
int top = -1;
// Push elements
if (top < MAX - 1) top++;
stack[top] = 10;
if (top < MAX - 1) top++;
stack[top] = 20;
// Check if full
if (top == MAX - 1) printf("Full"); else printf("Not Full");
What will be printed?Solution
Step 1: Trace the push operations
Initially,top = -1. First push increments top to 0 and stores 10. Second push increments top to 1 and stores 20.Step 2: Check if stack is full
Full meanstop == MAX - 1which is 2. Here, top is 1, so stack is not full.Final Answer:
Not Full -> Option AQuick Check:
Top is 1, max index 2, so not full [OK]
- Assuming two pushes fill the stack
- Confusing top index with count
- Ignoring initial top = -1
int isEmpty(int top) {
if (top == 0)
return 1;
else
return 0;
}Solution
Step 1: Understand stack empty condition
In stack implementation, empty meanstop == -1, not 0.Step 2: Identify the incorrect condition
The function checkstop == 0, which means stack has one element, so it's wrong.Final Answer:
It should check if top == -1 instead of 0 -> Option BQuick Check:
Empty stack top is -1, not 0 [OK]
- Checking top == 0 for empty
- Confusing full and empty conditions
- Returning wrong boolean values
top = 3. You want to push two elements safely. Which sequence of checks and operations is correct to avoid overflow?Solution
Step 1: Understand stack capacity and top indexing
Stack capacity is 5, so max top index is 4 (0 to 4).Step 2: Check condition before pushing
Before each push, check iftop == 4(full). If not full, increment top and push.Final Answer:
Check if top == 4 before each push; if false, increment top and push -> Option DQuick Check:
Max index is capacity-1 = 4, check top == 4 [OK]
- Checking top == capacity (off by one)
- Not checking before push causes overflow
- Assuming top == 3 means full
Ready to prove it?
Pass the test and this pattern moves unknown
