Check if Stack is Empty or Full in DSA Rust - 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.
Analyze the time complexity of the following code snippet.
struct Stack {
data: Vec<i32>,
capacity: usize,
}
impl Stack {
fn is_empty(&self) -> bool {
self.data.is_empty()
}
fn is_full(&self) -> bool {
self.data.len() == self.capacity
}
}
This code checks if the stack has no items or if it reached its max size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the length of the vector or if it is empty.
- How many times: These checks happen once per call, no loops or recursion.
Checking if the stack is empty or full takes the same time no matter the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same even if the stack grows.
Time Complexity: O(1)
This means checking if the stack is empty or full takes the same short time no matter how many items are inside.
[X] Wrong: "Checking if a stack is full or empty takes longer as the stack grows."
[OK] Correct: The check only looks at the count or emptiness, which is stored and accessed instantly, not by scanning all items.
Knowing these checks are very fast helps you explain stack operations clearly and confidently in interviews.
"What if the stack was implemented using a linked list instead of a vector? How would the time complexity of checking if it is full or empty change?"
Practice
is_empty() method of a stack typically check?Solution
Step 1: Understand the purpose of
This method checks whether the stack contains any elements or not.is_empty()Step 2: Interpret the meaning of empty stack
An empty stack means it has zero elements inside, sois_empty()returns true only in this case.Final Answer:
If the stack has no elements inside -> Option DQuick Check:
Empty stack means no elements [OK]
- Confusing empty with full
- Thinking empty means one element
- Assuming empty means stack is null
stack is full assuming it has a fixed capacity stored in capacity and current size in size?Solution
Step 1: Identify how to get current stack size
In Rust,len()method is commonly used to get the current number of elements in a collection like a stack.Step 2: Compare current size with capacity
Ifstack.len()equalscapacity, the stack is full.Final Answer:
if stack.len() == capacity { /* full */ } -> Option AQuick Check:
Uselen()to check size [OK]
- Using stack.size instead of stack.len()
- Checking size > capacity instead of == capacity
- Assuming is_full() method exists by default
struct Stack {
elements: Vec<i32>,
capacity: usize,
}
impl Stack {
fn is_full(&self) -> bool {
self.elements.len() == self.capacity
}
fn is_empty(&self) -> bool {
self.elements.is_empty()
}
}
fn main() {
let stack = Stack { elements: vec![1, 2, 3], capacity: 3 };
println!("{} {}", stack.is_empty(), stack.is_full());
}Solution
Step 1: Check if stack is empty
The stack has elements [1, 2, 3], sois_empty()returns false.Step 2: Check if stack is full
The stack capacity is 3 and current length is 3, sois_full()returns true.Final Answer:
false true -> Option AQuick Check:
Elements present = not empty, length == capacity = full [OK]
- Thinking stack is empty when it has elements
- Confusing is_empty() and is_full() return values
- Ignoring capacity when checking full
struct Stack {
elements: Vec<i32>,
}
impl Stack {
fn is_empty(&self) -> bool {
self.elements.len() = 0
}
}Solution
Step 1: Check the comparison operator in is_empty
The code uses single '=' which is assignment, not comparison. It should use '==' to compare length with zero.Step 2: Confirm other parts are correct
The return type bool is present, Vec<i32> is valid, and self does not need to be mutable for this check.Final Answer:
Using '=' instead of '==' for comparison in is_empty method -> Option CQuick Check:
Use '==' for comparison, not '=' [OK]
- Using '=' instead of '==' in conditions
- Forgetting return type bool
- Making self mutable unnecessarily
can_push that returns true if the stack is not full and false otherwise. Which implementation is correct?
struct Stack {
elements: Vec<i32>,
capacity: usize,
}
impl Stack {
fn can_push(&self) -> bool {
// Choose correct condition
}
}Solution
Step 1: Understand when pushing is allowed
You can push only if the stack is not full, meaning current size is less than capacity.Step 2: Choose the correct comparison
If length is less than capacity, pushing is possible. If equal or more, stack is full or invalid.Final Answer:
self.elements.len() < self.capacity -> Option BQuick Check:
Push allowed if size < capacity [OK]
- Using <= instead of < allows overflow
- Checking == means no room left
- Using > is logically incorrect
Ready to prove it?
Pass the test and this pattern moves unknown
