Bird
Raised Fist0
DSA Cprogramming~20 mins

Check if Stack is Empty or Full in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Stack Empty Check
What is the output of this C code that checks if the stack is empty after some push and pop operations?
DSA C
#include <stdio.h>
#define MAX 3

int stack[MAX];
int top = -1;

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

void push(int val) {
    if (top < MAX - 1) {
        stack[++top] = val;
    }
}

void pop() {
    if (!isEmpty()) {
        top--;
    }
}

int main() {
    push(10);
    push(20);
    pop();
    pop();
    if (isEmpty()) {
        printf("Stack is empty\n");
    } else {
        printf("Stack is not empty\n");
    }
    return 0;
}
ACompilation error
BStack is not empty
CRuntime error
DStack is empty
Attempts:
2 left
💡 Hint
Think about how many elements are pushed and popped before the check.
❓ Predict Output
intermediate
2:00remaining
Output of Stack Full Check
What is the output of this C code that checks if the stack is full after pushing elements?
DSA C
#include <stdio.h>
#define MAX 2

int stack[MAX];
int top = -1;

int isFull() {
    return top == MAX - 1;
}

void push(int val) {
    if (!isFull()) {
        stack[++top] = val;
    }
}

int main() {
    push(5);
    push(15);
    if (isFull()) {
        printf("Stack is full\n");
    } else {
        printf("Stack is not full\n");
    }
    return 0;
}
AStack is full
BStack is not full
CRuntime error
DCompilation error
Attempts:
2 left
💡 Hint
Check how many elements the stack can hold and how many are pushed.
❓ Predict Output
advanced
2:00remaining
Output after Mixed Stack Operations
What is the output of this C code after performing push and pop operations and checking if the stack is empty or full?
DSA C
#include <stdio.h>
#define MAX 4

int stack[MAX];
int top = -1;

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

int isFull() {
    return top == MAX - 1;
}

void push(int val) {
    if (!isFull()) {
        stack[++top] = val;
    }
}

void pop() {
    if (!isEmpty()) {
        top--;
    }
}

int main() {
    push(1);
    push(2);
    push(3);
    pop();
    push(4);
    push(5);
    if (isFull()) {
        printf("Stack is full\n");
    } else if (isEmpty()) {
        printf("Stack is empty\n");
    } else {
        printf("Stack is neither full nor empty\n");
    }
    return 0;
}
ARuntime error
BStack is full
CStack is empty
DStack is neither full nor empty
Attempts:
2 left
💡 Hint
Count the number of elements after all operations and compare with MAX.
❓ Predict Output
advanced
2:00remaining
Stack State After Invalid Push
What is the output of this C code after trying to push an element when the stack is already full, then checking if the stack is full?
DSA C
#include <stdio.h>
#define MAX 2

int stack[MAX];
int top = -1;

int isFull() {
    return top == MAX - 1;
}

void push(int val) {
    if (!isFull()) {
        stack[++top] = val;
    }
}

int main() {
    push(100);
    push(200);
    push(300); // This push should be ignored
    if (isFull()) {
        printf("Stack is full\n");
    } else {
        printf("Stack is not full\n");
    }
    return 0;
}
AStack is full
BStack is not full
CRuntime error
DCompilation error
Attempts:
2 left
💡 Hint
The third push should not add an element because the stack is full.
❓ Predict Output
expert
2:00remaining
Output of Stack Empty and Full Checks with Underflow and Overflow Attempts
What is the output of this C code that attempts to pop from an empty stack and push beyond full capacity, then checks if the stack is empty or full?
DSA C
#include <stdio.h>
#define MAX 3

int stack[MAX];
int top = -1;

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

int isFull() {
    return top == MAX - 1;
}

void push(int val) {
    if (!isFull()) {
        stack[++top] = val;
    }
}

void pop() {
    if (!isEmpty()) {
        top--;
    }
}

int main() {
    pop(); // pop on empty stack, should do nothing
    push(10);
    push(20);
    push(30);
    push(40); // push beyond full, ignored
    pop();
    pop();
    if (isEmpty()) {
        printf("Stack is empty\n");
    } else if (isFull()) {
        printf("Stack is full\n");
    } else {
        printf("Stack is neither full nor empty\n");
    }
    return 0;
}
AStack is empty
BStack is full
CStack is neither full nor empty
DRuntime error
Attempts:
2 left
💡 Hint
Track the stack size after each operation carefully.

Practice

(1/5)
1. What does the function isEmpty() typically check in a stack data structure?
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 has more than one element

Solution

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

    The isEmpty() function checks whether the stack contains any elements or not.
  2. Step 2: Identify what 'empty' means for a stack

    A stack is empty when it has zero elements, meaning no data is stored inside.
  3. Final Answer:

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

    Empty stack means zero elements = true [OK]
Hint: Empty stack means no elements inside [OK]
Common Mistakes:
  • Confusing empty with full
  • Thinking empty means one element
  • Mixing up stack size with capacity
2. Which of the following is the correct syntax to check if a stack is full in C, assuming top is the index of the top element and MAX is the stack capacity?
easy
A. if (top == MAX - 1)
B. if (top < MAX - 1)
C. if (top > MAX)
D. if (top == MAX)

Solution

  1. Step 1: Understand stack indexing

    Stack indices start at 0, so the last valid index is MAX - 1.
  2. Step 2: Check condition for full stack

    The stack is full when top points to the last index, which is MAX - 1.
  3. Final Answer:

    if (top == MAX - 1) -> Option A
  4. Quick Check:

    Top equals max index means full [OK]
Hint: Stack full when top equals MAX minus one [OK]
Common Mistakes:
  • Using top == MAX (off by one error)
  • Using top > MAX (invalid index)
  • Using top < MAX - 1 (means not full)
3. Consider this C code snippet for a stack with MAX = 3:
int stack[MAX];
int top = -1;

// Push elements
if (top < MAX - 1) top++;
stack[top] = 10;
if (top < MAX - 1) top++;
stack[top] = 20;

// Check if full
if (top == MAX - 1) printf("Full"); else printf("Not Full");
What will be printed?
medium
A. Not Full
B. Full
C. Runtime Error
D. Compilation Error

Solution

  1. Step 1: Trace the push operations

    Initially, top = -1. First push increments top to 0 and stores 10. Second push increments top to 1 and stores 20.
  2. Step 2: Check if stack is full

    Full means top == MAX - 1 which is 2. Here, top is 1, so stack is not full.
  3. Final Answer:

    Not Full -> Option A
  4. Quick Check:

    Top is 1, max index 2, so not full [OK]
Hint: Full means top equals MAX-1 index [OK]
Common Mistakes:
  • Assuming two pushes fill the stack
  • Confusing top index with count
  • Ignoring initial top = -1
4. Find the error in this C function that checks if a stack is empty:
int isEmpty(int top) {
    if (top == 0)
        return 1;
    else
        return 0;
}
medium
A. It should check if top == MAX - 1
B. It should check if top == -1 instead of 0
C. It should return 0 when empty
D. No error, function is correct

Solution

  1. Step 1: Understand stack empty condition

    In stack implementation, empty means top == -1, not 0.
  2. Step 2: Identify the incorrect condition

    The function checks top == 0, which means stack has one element, so it's wrong.
  3. Final Answer:

    It should check if top == -1 instead of 0 -> Option B
  4. Quick Check:

    Empty stack top is -1, not 0 [OK]
Hint: Empty stack top is -1, not zero [OK]
Common Mistakes:
  • Checking top == 0 for empty
  • Confusing full and empty conditions
  • Returning wrong boolean values
5. You have a stack with capacity 5 and top = 3. You want to push two elements safely. Which sequence of checks and operations is correct to avoid overflow?
hard
A. No need to check; just increment top and push
B. Check if top == 5 before each push; if false, increment top and push
C. Check if top == 3 before each push; if false, increment top and push
D. Check if top == 4 before each push; if false, increment top and push

Solution

  1. Step 1: Understand stack capacity and top indexing

    Stack capacity is 5, so max top index is 4 (0 to 4).
  2. Step 2: Check condition before pushing

    Before each push, check if top == 4 (full). If not full, increment top and push.
  3. Final Answer:

    Check if top == 4 before each push; if false, increment top and push -> Option D
  4. Quick Check:

    Max index is capacity-1 = 4, check top == 4 [OK]
Hint: Check top equals capacity minus one before push [OK]
Common Mistakes:
  • Checking top == capacity (off by one)
  • Not checking before push causes overflow
  • Assuming top == 3 means full
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