Bird
Raised Fist0
DSA C++programming~20 mins

Stack Concept and LIFO Principle 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 of this stack operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
DSA C++
std::stack<int> s;
s.push(10);
s.push(20);
s.pop();
s.push(30);
s.pop();
s.push(40);
s.push(50);
s.pop();
// Print stack from top to bottom
A20 -> 10 -> null
B50 -> 40 -> 10 -> null
C40 -> 10 -> null
D30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember pop removes the top element. Trace each step carefully.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes the LIFO principle in stacks?
Choose the statement that best explains how the Last In First Out (LIFO) principle works in a stack.
AElements are removed in the reverse order they were added.
BThe first element added is the first one to be removed.
CElements are removed randomly regardless of insertion order.
DThe last element added is never removed.
Attempts:
2 left
💡 Hint
Think about which element is on top and removed first.
🔧 Debug
advanced
1:30remaining
What error does this stack code produce?
Given the code below, what error will occur when running it?
std::stack s;
s.push(1);
s.pop();
s.pop();
DSA C++
std::stack<int> s;
s.push(1);
s.pop();
s.pop();
ALogic error: stack size incorrect
BRuntime error: popping from empty stack
CCompile error: pop() not defined
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check what happens if you pop more times than you push.
🚀 Application
advanced
1:30remaining
How many elements remain after this sequence?
A stack starts empty. We perform:
push(5), push(10), push(15), pop(), push(20), pop(), pop()
How many elements are left in the stack?
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Count pushes and pops carefully.
❓ Predict Output
expert
2:00remaining
What is printed by this C++ stack code?
What is the output of this code?
#include <iostream>
#include <stack>

int main() {
    std::stack s;
    for (int i = 1; i <= 3; ++i) s.push(i);
    int sum = 0;
    while (!s.empty()) {
        sum += s.top();
        s.pop();
    }
    std::cout << sum << std::endl;
    return 0;
}
DSA C++
#include <iostream>
#include <stack>

int main() {
    std::stack<int> s;
    for (int i = 1; i <= 3; ++i) s.push(i);
    int sum = 0;
    while (!s.empty()) {
        sum += s.top();
        s.pop();
    }
    std::cout << sum << std::endl;
    return 0;
}
A6
B3
C0
D9
Attempts:
2 left
💡 Hint
Sum all elements popped from the stack.

Practice

(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. Last In, First Out - the last element added is removed first
B. First In, First Out - the first element added is removed first
C. Elements are removed randomly
D. Elements are removed based on their value

Solution

  1. Step 1: Understand LIFO principle

    LIFO means the last element added to the stack is the first one to be removed.
  2. Step 2: Match with stack behavior

    Stacks follow LIFO, so the last pushed element is popped first.
  3. Final Answer:

    Last In, First Out - the last element added is removed first -> Option A
  4. Quick Check:

    LIFO = Last In, First Out [OK]
Hint: Remember stack removes newest item first [OK]
Common Mistakes:
  • Confusing LIFO with FIFO
  • Thinking stack removes oldest element first
  • Assuming random removal order
2. Which of the following is the correct way to push an integer 10 onto a stack named s in C++ STL?
easy
A. s.add(10);
B. s.insert(10);
C. push(s, 10);
D. s.push(10);

Solution

  1. Step 1: Recall C++ STL stack push syntax

    In C++ STL, the method to add an element to stack is push() called on the stack object.
  2. Step 2: Verify correct method usage

    Using s.push(10); correctly pushes 10 onto stack s.
  3. Final Answer:

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

    Stack push method = push() [OK]
Hint: Use s.push(value) to add in C++ STL stack [OK]
Common Mistakes:
  • Using non-existent methods like add or insert
  • Calling push as a free function
  • Confusing vector methods with stack
3. What will be the output of the following C++ code?
std::stack<int> s;
s.push(5);
s.push(10);
s.push(15);
int x = s.top();
s.pop();
int y = s.top();
std::cout << x << " " << y << std::endl;
medium
A. 15 10
B. 5 10
C. 10 5
D. 15 5

Solution

  1. Step 1: Trace stack push operations

    Elements pushed in order: 5 (bottom), 10, 15 (top).
  2. Step 2: Check top and pop operations

    First, x = s.top() gives 15. Then s.pop() removes 15. Next, y = s.top() gives 10.
  3. Final Answer:

    15 10 -> Option A
  4. Quick Check:

    Top after push = 15, after pop = 10 [OK]
Hint: Top shows last pushed; pop removes it [OK]
Common Mistakes:
  • Confusing order of elements after pop
  • Assuming pop returns value
  • Mixing bottom and top elements
4. Identify the error in the following C++ code snippet using stack:
std::stack<int> s;
s.pop();
s.push(20);
medium
A. Syntax error in stack declaration
B. Cannot pop from an empty stack; causes runtime error
C. push should come before pop always
D. No error; code runs fine

Solution

  1. Step 1: Analyze stack operations order

    The code calls pop() on an empty stack before pushing any element.
  2. Step 2: Understand pop behavior on empty stack

    Calling pop() on empty stack is undefined and causes runtime error.
  3. Final Answer:

    Cannot pop from an empty stack; causes runtime error -> Option B
  4. Quick Check:

    Pop empty stack = runtime error [OK]
Hint: Never pop before pushing any element [OK]
Common Mistakes:
  • Ignoring empty stack before pop
  • Assuming pop returns value
  • Thinking push order fixes pop error
5. Given a stack with elements pushed in this order: 3, 7, 9, 2, 5. Which sequence of pop() operations will produce the output 5 2 9 when printing popped elements?
hard
A. Pop twice, push 1, then pop once
B. Pop once, push 4, then pop twice
C. Pop three times consecutively
D. Pop once, pop again, then push 6 and pop

Solution

  1. Step 1: Understand stack order after pushes

    Stack top to bottom: 5 (top), 2, 9, 7, 3 (bottom).
  2. Step 2: Pop three times consecutively

    First pop: 5, second pop: 2, third pop: 9. This matches output sequence.
  3. Final Answer:

    Pop three times consecutively -> Option C
  4. Quick Check:

    Pop order matches output 5 2 9 [OK]
Hint: Pop in order to get last pushed elements first [OK]
Common Mistakes:
  • Adding extra pushes changes output
  • Popping fewer times than needed
  • Confusing push/pop order
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