Bird
Raised Fist0
DSA Javaprogramming

Check if Stack is Empty or Full in DSA Java

Choose your learning style10 modes available
Mental Model
A stack is like a pile where you add or remove items only from the top. Checking if it is empty means seeing if there is nothing in the pile, and checking if it is full means seeing if you reached the pile's limit.
Analogy: Imagine a stack of plates in a cupboard. If there are no plates, the stack is empty. If the cupboard is full and cannot hold more plates, the stack is full.
Top -> [item3] -> [item2] -> [item1] -> null
Index:    2        1        0

Empty stack:
Top -> null
Dry Run Walkthrough
Input: Stack with max size 3, current items: 2 (values: 10, 20)
Goal: Check if the stack is empty or full at different states
Step 1: Check if stack is empty
Top -> [20] -> [10] -> null
Why: We check if the top pointer is -1 to know if stack has no items
Step 2: Check if stack is full
Top -> [20] -> [10] -> null
Why: We check if top pointer equals max size - 1 to know if stack is full
Step 3: Push value 30 to stack
Top -> [30] -> [20] -> [10] -> null
Why: Add item on top and increase top pointer
Step 4: Check if stack is full again
Top -> [30] -> [20] -> [10] -> null
Why: Now top pointer equals max size - 1, stack is full
Result:
Top -> [30] -> [20] -> [10] -> null
Stack is not empty
Stack is not full
After push, stack is full
Annotated Code
DSA Java
public class Stack {
    private int maxSize;
    private int[] stackArray;
    private int top;

    public Stack(int size) {
        maxSize = size;
        stackArray = new int[maxSize];
        top = -1; // empty stack
    }

    public boolean isEmpty() {
        return top == -1; // top -1 means no items
    }

    public boolean isFull() {
        return top == maxSize - 1; // top at max index means full
    }

    public void push(int value) {
        if (!isFull()) {
            top++;
            stackArray[top] = value; // add item on top
        } else {
            System.out.println("Stack is full, cannot push " + value);
        }
    }

    public void printStack() {
        for (int i = top; i >= 0; i--) {
            System.out.print(stackArray[i] + " -> ");
        }
        System.out.println("null");
    }

    public static void main(String[] args) {
        Stack stack = new Stack(3);
        stack.push(10);
        stack.push(20);

        stack.printStack();
        System.out.println("Is stack empty? " + stack.isEmpty());
        System.out.println("Is stack full? " + stack.isFull());

        stack.push(30);
        stack.printStack();
        System.out.println("Is stack full after push? " + stack.isFull());
    }
}
return top == -1; // top -1 means no items
Check if stack is empty by seeing if top pointer is -1
return top == maxSize - 1; // top at max index means full
Check if stack is full by seeing if top pointer reached max index
top++; stackArray[top] = value; // add item on top
Increase top pointer and place new value on top
OutputSuccess
20 -> 10 -> null Is stack empty? false Is stack full? false 30 -> 20 -> 10 -> null Is stack full after push? true
Complexity Analysis
Time: O(1) because checking empty or full is just comparing the top pointer
Space: O(n) because stack uses an array of fixed size n
vs Alternative: Compared to dynamic stack implementations, fixed size stack uses constant time checks but limited capacity
Edge Cases
Empty stack
isEmpty returns true, isFull returns false
DSA Java
return top == -1; // top -1 means no items
Full stack
isFull returns true, push is prevented
DSA Java
if (!isFull()) { ... } else { System.out.println("Stack is full, cannot push " + value); }
When to Use This Pattern
When you see a problem asking if a stack can accept more items or if it has any items, reach for checking top pointer against boundaries because it tells empty or full instantly.
Common Mistakes
Mistake: Checking if stack is empty by comparing top to 0 instead of -1
Fix: Initialize top to -1 and check top == -1 for empty
Mistake: Checking if stack is full by comparing top to maxSize instead of maxSize - 1
Fix: Check top == maxSize - 1 because array indices start at 0
Summary
It checks if the stack has no items or is completely full by looking at the top pointer.
Use it when you want to know if you can add or remove items from the stack safely.
Remember: top == -1 means empty, top == maxSize - 1 means full.

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