Bird
Raised Fist0
DSA Javaprogramming~10 mins

Check if Stack is Empty or Full in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Check if Stack is Empty or Full
Start
↓
Check if stack pointer == -1?
Yes→Stack is Empty
|No↓
Check if stack pointer == max size - 1?
Yes→Stack is Full
|No↓
Stack has space and elements
↓
End
We first check if the stack pointer is -1 to see if the stack is empty. If not empty, we check if the pointer reached max size - 1 to see if it is full.
Execution Sample
DSA Java
int top = -1;
int maxSize = 3;

// Check empty
boolean isEmpty = (top == -1);
// Check full
boolean isFull = (top == maxSize - 1);
This code checks if the stack is empty or full by comparing the top pointer with -1 and maxSize - 1.
Execution Table
StepOperationtop PointermaxSizeIs Empty?Is Full?Visual Stack State
1Initialize top to -1-13truefalse[] (empty)
2Push element 10 (top++)03falsefalse[10]
3Push element 20 (top++)13falsefalse[10, 20]
4Push element 30 (top++)23falsetrue[10, 20, 30]
5Check if empty (top == -1)23falsetrue[10, 20, 30]
6Check if full (top == maxSize - 1)23falsetrue[10, 20, 30]
7Pop element (top--)13falsefalse[10, 20]
8Check if empty (top == -1)13falsefalse[10, 20]
9Check if full (top == maxSize - 1)13falsefalse[10, 20]
10Pop element (top--)03falsefalse[10]
11Pop element (top--)-13truefalse[] (empty)
12Check if empty (top == -1)-13truefalse[] (empty)
13Check if full (top == maxSize - 1)-13truefalse[] (empty)
14EndN/AN/AN/AN/AFinal empty stack
15ExitN/AN/AStack empty or full checkedN/AN/A
💡 Execution stops after checking empty/full states and performing push/pop operations within stack size limits.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 7After Step 10After Step 11Final
top-101210-1-1
maxSize33333333
Is Empty?truefalsefalsefalsefalsefalsetruetrue
Is Full?falsefalsefalsetruefalsefalsefalsefalse
Visual Stack State[][10][10, 20][10, 20, 30][10, 20][10][][]
Key Moments - 3 Insights
Why does the stack pointer start at -1 instead of 0?
The stack pointer starts at -1 to indicate the stack is empty before any push. This is shown in execution_table step 1 where top = -1 means empty stack.
How do we know the stack is full?
When the top pointer equals maxSize - 1, the stack is full. This is shown in step 4 where top = 2 and maxSize = 3, so stack is full.
What happens to the top pointer after popping all elements?
After popping all elements, the top pointer returns to -1, indicating the stack is empty again, as shown in step 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the value of 'Is Full?' and why?
Atrue, because top equals maxSize - 1
Bfalse, because top is less than maxSize - 1
Cfalse, because stack is empty
Dtrue, because top is -1
💡 Hint
Check the 'Is Full?' column and the 'top Pointer' value at step 4 in execution_table.
At which step does the stack become empty again after popping all elements?
AStep 7
BStep 10
CStep 11
DStep 4
💡 Hint
Look at the 'top Pointer' and 'Is Empty?' columns in execution_table after pop operations.
If maxSize was increased to 4, how would the 'Is Full?' value change at step 4?
AIt would be true because top is still 2
BIt would be false because top is less than maxSize - 1
CIt would be true because stack is empty
DIt would be false because top is -1
💡 Hint
Compare 'top Pointer' and 'maxSize - 1' values in execution_table step 4.
Concept Snapshot
Stack uses a pointer 'top' starting at -1.
Empty stack: top == -1.
Full stack: top == maxSize - 1.
Push increments top; pop decrements top.
Check empty/full by comparing top with -1 and maxSize - 1.
Visualize stack as array with top indicating last element.
Full Transcript
This visualization shows how to check if a stack is empty or full using the top pointer. The top pointer starts at -1 indicating an empty stack. When pushing elements, top increments. When popping, top decrements. The stack is empty if top equals -1 and full if top equals maxSize - 1. The execution table traces these changes step-by-step with the stack's visual state. Key moments clarify why top starts at -1, how full is detected, and what happens after popping all elements. The quiz tests understanding of these states and pointer values.

Practice

(1/5)
1. What does the isEmpty() method in a stack typically check?
easy
A. If the stack has exactly one element
B. If the stack is full
C. If the stack has no elements
D. If the stack is initialized

Solution

  1. Step 1: Understand the purpose of isEmpty()

    The isEmpty() method checks whether the stack contains any elements or not.
  2. Step 2: Identify the condition checked

    If the stack has no elements, it is empty, so isEmpty() returns true.
  3. Final Answer:

    If the stack has no elements -> Option C
  4. Quick Check:

    Empty stack means no elements = true [OK]
Hint: Empty means zero elements in stack [OK]
Common Mistakes:
  • Confusing isEmpty() with isFull()
  • Thinking isEmpty() checks for one element
  • Assuming isEmpty() checks initialization
2. Which of the following is the correct syntax to check if a stack named stack is full in Java?
easy
A. stack.isEmpty();
B. stack.isFull();
C. stack.full();
D. stack.isFilled();

Solution

  1. Step 1: Identify the standard method name for full check

    In stack implementations, the method to check if the stack is full is usually named isFull().
  2. Step 2: Match the correct syntax

    The correct syntax to call this method on a stack object named stack is stack.isFull();.
  3. Final Answer:

    stack.isFull(); -> Option B
  4. Quick Check:

    Method to check full = isFull() [OK]
Hint: Full check method is isFull() [OK]
Common Mistakes:
  • Using isEmpty() to check full
  • Using non-existent methods like full()
  • Misspelling method names
3. What will be the output of the following Java code?
class Stack {
  int maxSize = 3;
  int top = -1;
  int[] arr = new int[maxSize];

  boolean isFull() {
    return top == maxSize - 1;
  }

  boolean isEmpty() {
    return top == -1;
  }

  void push(int val) {
    if (!isFull()) {
      arr[++top] = val;
      System.out.println("Pushed " + val);
    } else {
      System.out.println("Stack is full");
    }
  }
}

public class Main {
  public static void main(String[] args) {
    Stack stack = new Stack();
    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);
  }
}
medium
A. Pushed 10 Pushed 20 Pushed 30 Stack is full
B. Pushed 10 Pushed 20 Pushed 30 Pushed 40
C. Stack is full Stack is full Stack is full Stack is full
D. Pushed 10 Pushed 20 Stack is full Stack is full

Solution

  1. Step 1: Trace push operations and stack state

    Initially, top = -1. Each push increments top and adds value if stack not full. Max size is 3, so max top index is 2.
  2. Step 2: Check each push call

    Push 10: top becomes 0, prints "Pushed 10".
    Push 20: top becomes 1, prints "Pushed 20".
    Push 30: top becomes 2, prints "Pushed 30".
    Push 40: top is 2 (full), so prints "Stack is full".
  3. Final Answer:

    Pushed 10 Pushed 20 Pushed 30 Stack is full -> Option A
  4. Quick Check:

    Stack max size 3, 4th push fails [OK]
Hint: Stack full when top == maxSize-1 [OK]
Common Mistakes:
  • Assuming push always succeeds
  • Ignoring top index limits
  • Confusing isEmpty() with isFull()
4. Find the error in this stack implementation snippet that checks if the stack is empty:
public boolean isEmpty() {
  if (top == 0) {
    return true;
  } else {
    return false;
  }
}
medium
A. No error, the code is correct
B. The method should return false when top == 0
C. The method should check if top == maxSize
D. The condition should be top == -1 to check empty

Solution

  1. Step 1: Understand stack empty condition

    In stack, empty means no elements pushed, so top is usually -1.
  2. Step 2: Identify incorrect condition

    The code checks top == 0, which means one element is present, so it is wrong for empty check.
  3. Final Answer:

    The condition should be top == -1 to check empty -> Option D
  4. Quick Check:

    Empty stack top = -1 [OK]
Hint: Empty stack top is -1, not 0 [OK]
Common Mistakes:
  • Checking top == 0 for empty
  • Confusing full and empty conditions
  • Returning wrong boolean values
5. You have a stack with max size 5 and current top = 3. Which of the following statements is true about the stack's state?
hard
A. The stack is neither full nor empty
B. The stack is empty
C. The stack is full
D. The stack has exactly 5 elements

Solution

  1. Step 1: Understand stack indexing and size

    Stack max size is 5, so valid top indices are 0 to 4. top = 3 means 4 elements are present (indices 0,1,2,3).
  2. Step 2: Determine if stack is full or empty

    Full means top == maxSize - 1 which is 4 here, so not full.
    Empty means top == -1, so not empty.
    Therefore, stack is neither full nor empty.
  3. Final Answer:

    The stack is neither full nor empty -> Option A
  4. Quick Check:

    top=3, maxSize=5 means partial fill [OK]
Hint: Full if top = maxSize-1, empty if top = -1 [OK]
Common Mistakes:
  • Assuming top=3 means full
  • Confusing top index with element count
  • Thinking top=3 means empty
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard