Stack Concept and LIFO Principle in DSA C# - Time & Space Complexity
We want to understand how fast stack operations run as the stack grows.
How does the time to add or remove items change when the stack gets bigger?
Analyze the time complexity of the following code snippet.
public class SimpleStack
{
private List<int> items = new List<int>();
public void Push(int item)
{
items.Add(item); // Add item to the top
}
public int Pop()
{
int top = items[items.Count - 1];
items.RemoveAt(items.Count - 1); // Remove top item
return top;
}
}
This code adds (push) and removes (pop) items from the end of a list to behave like a stack.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one item at the end of the list.
- How many times: Each push or pop does this once, no loops inside these methods.
Each push or pop does a fixed amount of work regardless of stack size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per push or pop |
| 100 | 1 operation per push or pop |
| 1000 | 1 operation per push or pop |
Pattern observation: The time stays the same no matter how many items are in the stack.
Time Complexity: O(1)
This means each push or pop takes the same short time no matter how big the stack is.
[X] Wrong: "Removing an item from the stack takes longer if the stack is bigger."
[OK] Correct: Because we only remove or add at the end, the operation does not depend on stack size.
Knowing that stack operations run in constant time shows you understand efficient data handling.
"What if we used a linked list instead of a list for the stack? How would the time complexity change?"
Practice
Solution
Step 1: Understand LIFO meaning
LIFO means the last item you put in is the first one you take out.Step 2: Apply to stack behavior
Stacks follow LIFO, so the last pushed element is popped first.Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option CQuick Check:
LIFO = Last In, First Out [OK]
- Confusing LIFO with FIFO
- Thinking first added is removed first
- Mixing up the order of insertion and removal
myStack in C#?Solution
Step 1: Recall stack push method
In C#, the method to add an element to a stack is Push.Step 2: Check method names
Add and Insert are not stack methods; Enqueue is for queues.Final Answer:
myStack.Push(10); -> Option BQuick Check:
Push() adds element to stack [OK]
- Using Add() or Insert() which are not stack methods
- Confusing stack with queue methods like Enqueue()
- Forgetting parentheses in method call
Stack<int> stack = new Stack<int>(); stack.Push(5); stack.Push(10); stack.Push(15); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Peek());
Solution
Step 1: Trace Push operations
Elements pushed in order: 5 (bottom), 10, 15 (top).Step 2: Trace Pop and Peek
Pop removes and returns 15 (top). Peek returns new top 10 without removing.Final Answer:
15\n10 -> Option AQuick Check:
Pop() = 15, Peek() = 10 [OK]
- Mixing Pop and Peek outputs
- Assuming Pop does not remove element
- Confusing order of pushed elements
Stack<string> stack = new Stack<string>();
stack.Push("apple");
stack.Push("banana");
string fruit = stack.Pop();
string topFruit = stack.Top();Solution
Step 1: Check stack methods
Stack in C# has Peek() to view top element, not Top().Step 2: Validate other statements
Pop() returns string, Push() takes one argument, no capacity needed.Final Answer:
Stack does not have a method named Top() -> Option DQuick Check:
Use Peek() to view top element [OK]
- Using Top() instead of Peek()
- Thinking Pop() returns void
- Assuming Push() needs capacity argument
Solution
Step 1: Track initial pushes
Stack after pushes: bottom 1,2,3,4,5 top.Step 2: Pop two elements
Pop removes 5 then 4; stack now 1,2,3 (bottom to top).Step 3: Push 6 and 7
Stack now: 1,2,3,6,7 (bottom to top).Step 4: Pop all remaining elements
Pop order (first popped to last): 7,6,3,2,1.Final Answer:
7, 6, 3, 2, 1 -> Option AQuick Check:
Pop order after operations = 7,6,3,2,1 [OK]
- Forgetting to remove popped elements
- Mixing push and pop order
- Ignoring elements still in stack after operations
Ready to prove it?
Pass the test and this pattern moves unknown
