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 where we push integers 1, 2, 3 in order, then pop one element, then push 4. What is the printed stack from top to bottom?
DSA C#
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Pop();
stack.Push(4);

while(stack.Count > 0)
{
    Console.Write(stack.Pop());
    if(stack.Count > 0) Console.Write(" -> ");
}
Console.WriteLine(" -> null");
A4 -> 2 -> 1 -> null
B2 -> 4 -> 1 -> null
C1 -> 2 -> 4 -> null
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember that stack follows Last In First Out (LIFO). The last pushed element is popped first.
🧠 Conceptual
intermediate
1:00remaining
Which statement best describes the LIFO principle in stacks?
Choose the statement that correctly explains the Last In First Out (LIFO) principle used by stacks.
AThe first element added is the first one to be removed.
BElements are removed in random order regardless of insertion.
CThe last element added is the first one to be removed.
DElements are removed in the same order they were added.
Attempts:
2 left
💡 Hint
Think about which element is on top of the stack.
🔧 Debug
advanced
1:30remaining
What error occurs when popping from an empty stack?
Given this code snippet, what error will be raised when the Pop method is called on an empty stack?
DSA C#
Stack<int> stack = new Stack<int>();
stack.Pop();
AInvalidOperationException
BNo error, returns default value
CIndexOutOfRangeException
DNullReferenceException
Attempts:
2 left
💡 Hint
Check what exception Stack.Pop throws if empty.
🚀 Application
advanced
2:00remaining
What is the stack content after this sequence of operations?
Start with an empty stack. Perform these operations in order: Push(10), Push(20), Pop(), Push(30), Push(40), Pop(), Push(50). What is the stack content from top to bottom?
A50 -> 40 -> 10 -> null
B50 -> 30 -> 10 -> null
C40 -> 30 -> 10 -> null
D30 -> 50 -> 10 -> null
Attempts:
2 left
💡 Hint
Track each push and pop carefully, remembering LIFO.
❓ Predict Output
expert
2:30remaining
What is the output of this C# stack iteration code?
Given this code, what is printed to the console?
DSA C#
Stack<string> stack = new Stack<string>();
stack.Push("apple");
stack.Push("banana");
stack.Push("cherry");

foreach(var item in stack)
{
    Console.Write(item + " -> ");
}
Console.WriteLine("null");
Aapple -> cherry -> banana -> null
Bapple -> banana -> cherry -> null
Cbanana -> cherry -> apple -> null
Dcherry -> banana -> apple -> null
Attempts:
2 left
💡 Hint
Stack enumeration goes from top to bottom without removing elements.

Practice

(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. Last In, First Out - the first element added is the first to be removed
B. First In, First Out - the first element added is the first to be removed
C. Last In, First Out - the last element added is the first to be removed
D. First In, Last Out - the first element added is the last to be removed

Solution

  1. Step 1: Understand LIFO meaning

    LIFO means the last item you put in is the first one you take out.
  2. Step 2: Apply to 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 the first to be removed -> Option C
  4. Quick Check:

    LIFO = Last In, First Out [OK]
Hint: Remember stack is like a stack of plates, last added removed first [OK]
Common Mistakes:
  • Confusing LIFO with FIFO
  • Thinking first added is removed first
  • Mixing up the order of insertion and removal
2. Which of the following is the correct way to push an integer 10 onto a stack named myStack in C#?
easy
A. myStack.Enqueue(10);
B. myStack.Push(10);
C. myStack.Insert(10);
D. myStack.Add(10);

Solution

  1. Step 1: Recall stack push method

    In C#, the method to add an element to a stack is Push.
  2. Step 2: Check method names

    Add and Insert are not stack methods; Enqueue is for queues.
  3. Final Answer:

    myStack.Push(10); -> Option B
  4. Quick Check:

    Push() adds element to stack [OK]
Hint: Use Push() to add elements to stack in C# [OK]
Common Mistakes:
  • Using Add() or Insert() which are not stack methods
  • Confusing stack with queue methods like Enqueue()
  • Forgetting parentheses in method call
3. What will be the output of the following C# code?
Stack<int> stack = new Stack<int>();
stack.Push(5);
stack.Push(10);
stack.Push(15);
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
medium
A. 15 10
B. 5 10
C. 10 15
D. 15 15

Solution

  1. Step 1: Trace Push operations

    Elements pushed in order: 5 (bottom), 10, 15 (top).
  2. Step 2: Trace Pop and Peek

    Pop removes and returns 15 (top). Peek returns new top 10 without removing.
  3. Final Answer:

    15\n10 -> Option A
  4. Quick Check:

    Pop() = 15, Peek() = 10 [OK]
Hint: Pop returns top and removes it; Peek returns top without removing [OK]
Common Mistakes:
  • Mixing Pop and Peek outputs
  • Assuming Pop does not remove element
  • Confusing order of pushed elements
4. Identify the error in the following C# code snippet using a stack:
Stack<string> stack = new Stack<string>();
stack.Push("apple");
stack.Push("banana");
string fruit = stack.Pop();
string topFruit = stack.Top();
medium
A. Stack must be initialized with capacity
B. Pop() cannot be assigned to a string variable
C. Push() method requires two arguments
D. Stack does not have a method named Top()

Solution

  1. Step 1: Check stack methods

    Stack in C# has Peek() to view top element, not Top().
  2. Step 2: Validate other statements

    Pop() returns string, Push() takes one argument, no capacity needed.
  3. Final Answer:

    Stack does not have a method named Top() -> Option D
  4. Quick Check:

    Use Peek() to view top element [OK]
Hint: Use Peek() to see top element, not Top() [OK]
Common Mistakes:
  • Using Top() instead of Peek()
  • Thinking Pop() returns void
  • Assuming Push() needs capacity argument
5. Given a stack with elements pushed in this order: 1, 2, 3, 4, 5. You pop two elements, then push 6 and 7. What is the order of elements if you pop all remaining elements one by one?
hard
A. 7, 6, 3, 2, 1
B. 3, 2, 6, 7
C. 5, 4, 6, 7
D. 7, 6, 3, 2

Solution

  1. Step 1: Track initial pushes

    Stack after pushes: bottom 1,2,3,4,5 top.
  2. Step 2: Pop two elements

    Pop removes 5 then 4; stack now 1,2,3 (bottom to top).
  3. Step 3: Push 6 and 7

    Stack now: 1,2,3,6,7 (bottom to top).
  4. Step 4: Pop all remaining elements

    Pop order (first popped to last): 7,6,3,2,1.
  5. Final Answer:

    7, 6, 3, 2, 1 -> Option A
  6. Quick Check:

    Pop order after operations = 7,6,3,2,1 [OK]
Hint: Pop removes last pushed; track pushes and pops stepwise [OK]
Common Mistakes:
  • Forgetting to remove popped elements
  • Mixing push and pop order
  • Ignoring elements still in stack after operations
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