A stack is empty when it has no elements inside it. This means there is nothing to remove or peek at.
Click to reveal answer
beginner
How can you check if a stack is full in a fixed-size stack?
You check if the stack is full by comparing the current number of elements with the maximum size of the stack. If they are equal, the stack is full.
Click to reveal answer
beginner
TypeScript code snippet: How to check if a stack is empty?
if (stack.length === 0) {
// stack is empty
}
Click to reveal answer
beginner
TypeScript code snippet: How to check if a stack is full given max size?
if (stack.length === maxSize) {
// stack is full
}
Click to reveal answer
beginner
Why is it important to check if a stack is empty before popping?
Because popping from an empty stack causes an error or unexpected behavior. Checking prevents removing from an empty stack.
Click to reveal answer
What condition means a stack is empty?
Astack.length > 0
Bstack.length < maxSize
Cstack.length === maxSize
Dstack.length === 0
✗ Incorrect
A stack is empty when its length is zero, meaning no elements are inside.
How do you know a fixed-size stack is full?
Astack.length === maxSize
Bstack.length === 0
Cstack.length < maxSize
Dstack.length > maxSize
✗ Incorrect
When the number of elements equals the maximum size, the stack is full.
What happens if you pop from an empty stack?
AYou get the last element
BAn error or unexpected behavior occurs
CThe stack becomes full
DNothing happens
✗ Incorrect
Popping from an empty stack causes errors or unexpected results.
Which TypeScript code checks if a stack is empty?
Aif (stack.length > maxSize) {}
Bif (stack.length === maxSize) {}
Cif (stack.length === 0) {}
Dif (stack.length !== 0) {}
✗ Incorrect
Checking if stack.length is zero means the stack is empty.
Why check if a stack is full before pushing?
ATo avoid adding beyond capacity
BTo remove elements
CTo clear the stack
DTo check if stack is empty
✗ Incorrect
Checking prevents adding elements when the stack is already full.
Explain how to check if a stack is empty or full in TypeScript.
Think about the number of elements compared to zero and max size.
You got /3 concepts.
Why is it important to check stack state before popping or pushing?
Consider what happens if you ignore these checks.
You got /3 concepts.
Practice
(1/5)
1. What does the isEmpty() method check in a stack data structure?
easy
A. If the stack is full and cannot add more elements
B. If the stack has no elements inside
C. If the stack has exactly one element
D. If the stack has more than one element
Solution
Step 1: Understand the purpose of isEmpty()
The isEmpty() 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, so isEmpty() returns true only when the stack is empty.
Final Answer:
If the stack has no elements inside -> Option B
Quick Check:
isEmpty() checks for empty stack = A [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
Confusing isEmpty() with isFull()
Thinking isEmpty() checks for one element
Assuming isEmpty() checks if stack is full
2. Which of the following is the correct TypeScript syntax to check if a stack is full when using an array with fixed size maxSize and current top index?
easy
A. if (top === maxSize - 1) { /* stack is full */ }
B. if (top === maxSize) { /* stack is full */ }
C. if (top > maxSize) { /* stack is full */ }
D. if (top < maxSize) { /* stack is full */ }
Solution
Step 1: Understand stack indexing
In a fixed size stack using an array, top starts at -1 and increments by 1 for each push. The last valid index is maxSize - 1.
Step 2: Check condition for full stack
The stack is full when top equals maxSize - 1, meaning no more space to add elements.
Final Answer:
if (top === maxSize - 1) { /* stack is full */ } -> Option A
Quick Check:
Full stack when top = maxSize - 1 [OK]
Hint: Stack full when top equals maxSize minus one [OK]
Common Mistakes:
Using top === maxSize (off by one error)
Checking top > maxSize (invalid index)
Using top < maxSize (means not full)
3. Consider this TypeScript code for a stack with maxSize 3:
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 C
Quick Check:
Stack full at 3 items, 4th push ignored [OK]
Hint: Push stops when stack is full, extra items ignored [OK]
Common Mistakes:
Assuming push adds all items ignoring full check
Thinking array grows beyond maxSize
Confusing top index with array length
4. This TypeScript stack code tries to check if the stack is empty:
class Stack {
top = -1;
isEmpty() {
return this.top === 0;
}
}
What is wrong with this isEmpty() method?
medium
A. It should check if top is 1, not 0
B. It should check if top is less than 0
C. It should check if top is greater than 0
D. It should check if top is -1, not 0
Solution
Step 1: Understand top index for empty stack
In stack implementation, top is -1 when stack is empty (no elements pushed yet).
Step 2: Identify incorrect condition
The code checks top === 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 D
Quick Check:
Empty stack means top = -1 [OK]
Hint: Empty stack top is -1, not zero [OK]
Common Mistakes:
Checking top === 0 means stack has one element
Using greater than or less than instead of equality
Confusing empty with single element condition
5. You have a stack with maxSize 5 and current top at 3. You want to write a method canPush() that returns true only if the stack is not full and not empty. Which condition correctly implements this?