Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.