Bird
Raised Fist0
DSA Cprogramming~20 mins

Push Operation on Stack 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
What is the output after pushing elements on the stack?
Consider a stack implemented using an array with a maximum size of 5. Initially, the stack is empty. We perform the following push operations in order: push(10), push(20), push(30). What is the printed state of the stack after these operations?
DSA C
int stack[5];
int top = -1;

void push(int x) {
    if (top == 4) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = x;
}

void printStack() {
    for (int i = 0; i <= top; i++) {
        printf("%d -> ", stack[i]);
    }
    printf("null\n");
}

int main() {
    push(10);
    push(20);
    push(30);
    printStack();
    return 0;
}
AStack Overflow
B30 -> 20 -> 10 -> null
Cnull
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that push adds elements on top, and printStack prints from bottom to top index.
❓ Predict Output
intermediate
2:00remaining
What happens when pushing beyond stack capacity?
Given a stack with maximum size 3, initially empty, we push elements 5, 15, 25, and then try to push 35. What will be the output after these operations?
DSA C
int stack[3];
int top = -1;

void push(int x) {
    if (top == 2) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = x;
}

void printStack() {
    for (int i = 0; i <= top; i++) {
        printf("%d -> ", stack[i]);
    }
    printf("null\n");
}

int main() {
    push(5);
    push(15);
    push(25);
    push(35);
    printStack();
    return 0;
}
A
Stack Overflow
5 -&gt; 15 -&gt; 25 -&gt; null
B5 -> 15 -> 25 -> 35 -> null
C
Stack Overflow
null
D5 -> 15 -> null
Attempts:
2 left
💡 Hint
Check the condition that prevents pushing when stack is full.
🧠 Conceptual
advanced
1:30remaining
How does the push operation affect the 'top' pointer in a stack?
In a stack implemented using an array, what is the correct way the 'top' pointer changes when an element is pushed?
AAssign the new element at stack[top], then increment 'top' by 1
BIncrement 'top' by 1, then assign the new element at stack[top]
CDecrement 'top' by 1, then assign the new element at stack[top]
DAssign the new element at stack[top], then decrement 'top' by 1
Attempts:
2 left
💡 Hint
Think about where the new element should be placed relative to the current top.
🔧 Debug
advanced
2:00remaining
Identify the error in this push function implementation
What error will occur when running this push function on a stack of size 5?
DSA C
int stack[5];
int top = -1;

void push(int x) {
    if (top == 4) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = x;
}

int main() {
    push(1);
    push(2);
    push(3);
    return 0;
}
ACompilation error due to syntax
BStack Overflow message prints immediately
CWrites out of bounds and corrupts memory
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check how 'top' is used and updated in the push function.
🚀 Application
expert
3:00remaining
After a sequence of push and pop operations, what is the stack state?
A stack with max size 4 is initially empty. The following operations are performed: push(7), push(14), pop(), push(21), push(28), pop(), push(35). What is the printed stack state after these operations?
DSA C
int stack[4];
int top = -1;

void push(int x) {
    if (top == 3) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = x;
}

void pop() {
    if (top == -1) {
        printf("Stack Underflow\n");
        return;
    }
    top--;
}

void printStack() {
    for (int i = 0; i <= top; i++) {
        printf("%d -> ", stack[i]);
    }
    printf("null\n");
}

int main() {
    push(7);
    push(14);
    pop();
    push(21);
    push(28);
    pop();
    push(35);
    printStack();
    return 0;
}
A7 -> 21 -> 35 -> null
B7 -> 21 -> 28 -> 35 -> null
C7 -> 14 -> 21 -> 35 -> null
DStack Underflow
Attempts:
2 left
💡 Hint
Track the top index carefully after each push and pop.

Practice

(1/5)
1. What does the push operation do in a stack?
easy
A. Checks if the stack is empty
B. Adds an element to the top of the stack
C. Removes the top element from the stack
D. Returns the bottom element of the stack

Solution

  1. Step 1: Understand stack operations

    A stack is a data structure where elements are added or removed from the top only.
  2. Step 2: Define push operation

    The push operation adds a new element on top of the existing elements in the stack.
  3. Final Answer:

    Adds an element to the top of the stack -> Option B
  4. Quick Check:

    Push means add on top = C [OK]
Hint: Push always adds on top of stack [OK]
Common Mistakes:
  • Confusing push with pop operation
  • Thinking push removes elements
  • Mixing stack with queue operations
2. Which of the following is the correct syntax to push an element 5 onto a stack named s in C?
easy
A. s.push = 5;
B. s.push(5);
C. push(5, s);
D. push(s, 5);

Solution

  1. Step 1: Understand typical C stack push syntax

    In C, stack operations are usually functions where the stack is passed as the first argument.
  2. Step 2: Match syntax with parameters

    The correct function call to push 5 onto stack s is push(s, 5);.
  3. Final Answer:

    push(s, 5); -> Option D
  4. Quick Check:

    Function push(stack, value) = A [OK]
Hint: In C, function push(stack, value) is standard [OK]
Common Mistakes:
  • Using object.method syntax (not C style)
  • Swapping argument order
  • Assigning value instead of calling function
3. What will be the content of the stack after executing the following code?
int stack[5];
int top = -1;
// Push function
void push(int val) {
  if(top < 4) {
    top++;
    stack[top] = val;
  }
}

push(10);
push(20);
push(30);
medium
A. [10, 20, 30]
B. [30, 20, 10]
C. [10, 20]
D. Empty stack

Solution

  1. Step 1: Trace each push call

    Initially, top = -1. After push(10), top=0, stack[0]=10. After push(20), top=1, stack[1]=20. After push(30), top=2, stack[2]=30.
  2. Step 2: List stack content from bottom to top

    Stack elements from index 0 to top (2) are [10, 20, 30].
  3. Final Answer:

    [10, 20, 30] -> Option A
  4. Quick Check:

    Push adds at top increasing index = B [OK]
Hint: Stack stores pushed elements in order at increasing top index [OK]
Common Mistakes:
  • Reversing order of elements
  • Confusing top index with element values
  • Assuming stack is empty after pushes
4. Identify the error in the following push function for a stack of size 3:
int stack[3];
int top = 0;
void push(int val) {
  if(top <= 3) {
    stack[top] = val;
    top++;
  }
}
medium
A. Condition should be top < 3, not top <= 3
B. top should be incremented before assignment
C. stack array size should be 4
D. top should start at -1, not 0

Solution

  1. Step 1: Analyze top and condition

    top starts at 0, which is valid for empty stack at index 0. The condition allows top to be 3, which is out of bounds for stack[3] (indices 0 to 2).
  2. Step 2: Correct the condition

    Change condition to top < 3 to prevent overflow and invalid access.
  3. Final Answer:

    Condition should be top < 3, not top <= 3 -> Option A
  4. Quick Check:

    Stack max index 2 means top < 3 [OK]
Hint: Check array bounds: top must be less than size [OK]
Common Mistakes:
  • Allowing top to equal array size causing overflow
  • Starting top at -1 but not adjusting code
  • Incrementing top before assignment causing skip
5. Given a stack implemented with an array of size 4 and top = 2 currently, what will happen if we try to push two more elements without popping?
Choose the correct behavior.
hard
A. Both pushes fail immediately due to full stack
B. Both pushes succeed, stack size automatically increases
C. First push succeeds, second push causes stack overflow
D. First push causes overflow, second push is not attempted

Solution

  1. Step 1: Understand current stack state

    Array size is 4, so valid indices are 0 to 3. top=2 means top element is at index 2, so one slot remains at index 3.
  2. Step 2: Push two elements

    First push increments top to 3 and stores element at index 3 (valid). Second push tries to increment top to 4, which is out of bounds, causing overflow.
  3. Final Answer:

    First push succeeds, second push causes stack overflow -> Option C
  4. Quick Check:

    Array size 4, top max index 3, overflow after that [OK]
Hint: Stack size fixed; overflow after last index [OK]
Common Mistakes:
  • Assuming stack auto-expands
  • Thinking both pushes fail immediately
  • Confusing top index with count
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