Stack Concept and LIFO Principle in DSA Java - Time & Space Complexity
We want to understand how the time to perform stack operations changes as the number of items grows.
Specifically, how fast can we add or remove items following the Last In First Out rule?
Analyze the time complexity of the following stack operations.
public class Stack {
private int[] data;
private int top = -1;
public Stack(int size) {
data = new int[size];
}
public void push(int value) {
data[++top] = value;
}
public int pop() {
return data[top--];
}
}
This code shows how to add (push) and remove (pop) items from a stack using an array.
Look for repeated actions that take time.
- Primary operation: Adding or removing one item at the top of the stack.
- How many times: Each push or pop happens once per operation, no loops inside.
Each push or pop takes the same small amount of time no matter how many items are in the stack.
| Input Size (n) | Approx. Operations per push/pop |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even as the stack grows larger.
Time Complexity: O(1)
This means each push or pop operation takes a constant, fixed amount of time regardless of stack size.
[X] Wrong: "Pushing or popping takes longer as the stack gets bigger because there are more items."
[OK] Correct: Each operation only touches the top item, so it does not depend on how many items are below.
Understanding that stack operations are fast and simple helps you explain why stacks are useful in many programs and algorithms.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of push and pop change?"
Practice
Solution
Step 1: Understand the LIFO principle
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 BQuick Check:
LIFO = Last In, First Out [OK]
- Confusing LIFO with FIFO
- Thinking elements are removed randomly
- Assuming removal depends on element value
stack in Java?Solution
Step 1: Recall Java Stack method for adding elements
The method to add an element to a stack ispush().Step 2: Match method with given options
Onlystack.push(5);correctly uses the stack push method.Final Answer:
stack.push(5); -> Option DQuick Check:
Use push() to add elements to stack [OK]
- Using add() which is for lists, not stacks
- Using insert() or put() which are invalid for Stack
- Confusing stack methods with other collections
Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println(stack.pop()); System.out.println(stack.pop());
Solution
Step 1: Trace push operations
Elements pushed in order: 10 (bottom), 20, 30 (top).Step 2: Trace pop operations
First pop removes 30, second pop removes 20.Final Answer:
30\n20 -> Option AQuick Check:
Stack pops last pushed first: 30 then 20 [OK]
- Assuming pop removes from bottom
- Mixing order of popped elements
- Confusing push and pop effects
Stack<String> stack = new Stack<>();
stack.push("apple");
stack.push("banana");
String fruit = stack.pop();
System.out.println(stack.peek());
stack.pop();
stack.pop();Solution
Step 1: Trace stack operations
Push "apple", then "banana". Pop removes "banana". Peek shows "apple". Next pop removes "apple".Step 2: Identify error on last pop
Last pop tries to remove from empty stack, causing EmptyStackException.Final Answer:
Calling pop() on an empty stack causes EmptyStackException -> Option CQuick Check:
Pop on empty stack throws exception [OK]
- Ignoring exception on empty pop
- Thinking peek removes element
- Believing stack can't hold Strings
5 4 3 and leave the stack with elements 1 2 remaining?Solution
Step 1: Understand initial stack order
Stack top is 5, bottom is 1 after pushing 1 to 5.Step 2: Pop three times consecutively
Pop removes 5, then 4, then 3, leaving 1 and 2 in stack.Final Answer:
Pop three times consecutively -> Option AQuick Check:
3 pops remove top 3 elements [OK]
- Adding elements unnecessarily
- Confusing push and pop order
- Expecting output without popping
Ready to prove it?
Pass the test and this pattern moves unknown
