Check if Stack is Empty or Full in DSA Go - Time & Space Complexity
Checking if a stack is empty or full is a common operation in stack data structures.
We want to know how fast these checks happen as the stack size changes.
Analyze the time complexity of the following code snippet.
package main
import "fmt"
type Stack struct {
items []int
size int
top int
}
func (s *Stack) IsEmpty() bool {
return s.top == -1
}
func (s *Stack) IsFull() bool {
return s.top == s.size-1
}
func main() {
stack := Stack{items: make([]int, 5), size: 5, top: -1}
fmt.Println(stack.IsEmpty())
fmt.Println(stack.IsFull())
}
This code defines a stack and checks if it is empty or full by comparing the top index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple comparison of integers (top index with -1 or size-1).
- How many times: Each check runs once per call, no loops or recursion involved.
Checking if the stack is empty or full does not depend on the number of items in the stack.
| 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 happens instantly, no matter how many items are in the stack.
[X] Wrong: "Checking if the stack is full or empty takes longer as the stack grows."
[OK] Correct: The check only compares two numbers, so it always takes the same short time.
Knowing that these checks are very fast helps you focus on more complex 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() function check in a stack data structure?Solution
Step 1: Understand the purpose of IsEmpty()
The IsEmpty() function checks whether the stack contains any elements or not.Step 2: Identify what empty means for a stack
An empty stack means it has zero elements inside, so no items to pop or peek.Final Answer:
If the stack has no elements inside -> Option AQuick Check:
IsEmpty() means stack is empty = true [OK]
- Confusing empty with full
- Thinking empty means one element
- Mixing IsEmpty with IsFull
Solution
Step 1: Understand stack indexing in Go
For a fixed size array stack, top points to the last filled index. The last valid index is length - 1.Step 2: Check condition for full stack
When top equals len(stack) - 1, the stack is full because no more elements can be added.Final Answer:
if top == len(stack) - 1 { /* stack is full */ } -> Option CQuick Check:
top == max index means full [OK]
- Using > instead of == for full check
- Checking if top == 0 for full
- Confusing length with top index
type Stack struct {
items []int
top int
}
func (s *Stack) IsEmpty() bool {
return s.top == -1
}
func main() {
s := Stack{items: make([]int, 5), top: -1}
fmt.Println(s.IsEmpty())
s.top = 4
fmt.Println(s.IsEmpty())
}Solution
Step 1: Check initial top value and IsEmpty() result
Initially, top is -1, so IsEmpty() returns true because stack is empty.Step 2: Update top to 4 and check IsEmpty() again
After setting top to 4, IsEmpty() returns false because stack now has elements.Final Answer:
true\nfalse -> Option AQuick Check:
top -1 means empty true, top 4 means not empty false [OK]
- Assuming top 4 means empty
- Confusing slice length with top
- Ignoring initial top value
func (s *Stack) IsFull() bool {
return s.top == len(s.items)
}Solution
Step 1: Understand stack indexing for full condition
Stack top index starts at -1 and max valid index is length - 1.Step 2: Identify correct full condition
Full means top equals last valid index, which is len(s.items) - 1, not len(s.items).Final Answer:
The condition should be 's.top == len(s.items) - 1' to check full correctly -> Option BQuick Check:
Full means top == max index (length - 1) [OK]
- Using length instead of length - 1
- Checking top == 0 for full
- Returning wrong boolean condition
Solution
Step 1: Track stack operations and top index changes
Start: top = -1 (empty). Push two elements: top = 1. Pop one element: top = 0. Push two elements: top = 2 (max index for length 3).Step 2: Determine if stack is full
Since top = 2 and max index is 2 (length 3 - 1), stack is full.Final Answer:
The stack is full and top index is 2 -> Option DQuick Check:
Push/pop changes top; top 2 means full for size 3 [OK]
- Assuming top can be 3 for full
- Forgetting pop decreases top
- Confusing empty with full states
Ready to prove it?
Pass the test and this pattern moves unknown
