Push Operation on Stack in DSA Java - Time & Space Complexity
We want to understand how the time to add an item to a stack changes as the stack grows.
How much work does it take to push one element onto the stack?
Analyze the time complexity of the following code snippet.
public class Stack {
private int[] arr;
private int top;
public Stack(int size) {
arr = new int[size];
top = -1;
}
public void push(int value) {
arr[++top] = value;
}
}
This code adds a new value on top of the stack by increasing the top pointer and storing the value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning the new value to the array at the next top position.
- How many times: Exactly once per push call, no loops or recursion involved.
Each push does the same small amount of work no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the stack grows larger.
Time Complexity: O(1)
This means pushing an item takes the same short time no matter how many items are already in the stack.
[X] Wrong: "Pushing takes longer as the stack gets bigger because it has to move all the items."
[OK] Correct: The push operation only adds one item at the top without moving others, so the time does not grow with stack size.
Knowing that push is a quick, constant-time operation helps you explain stack efficiency clearly in interviews.
"What if the stack uses a dynamic array that resizes when full? How would the time complexity of push change then?"
Practice
push operation do in a stack?Solution
Step 1: Understand stack behavior
A stack follows Last In First Out (LIFO) principle, so new elements are added on top.Step 2: Define push operation
The push operation adds a new element to the top of the stack, increasing its size by one.Final Answer:
Adds an element to the top of the stack -> Option AQuick Check:
Push = Add top element [OK]
- Confusing push with pop
- Thinking push removes element
- Assuming push adds at bottom
stack in Java?Solution
Step 1: Recall Java Stack API
Java's Stack class uses the methodpush()to add elements.Step 2: Match correct method
Among options, onlypush()is the correct method to add elements to a stack.Final Answer:
stack.push(10); -> Option BQuick Check:
Java stack push method = push() [OK]
- Using add() which is for lists
- Using insert() which doesn't exist
- Using put() which is for maps
Stack<Integer> stack = new Stack<>(); stack.push(5); stack.push(10); stack.push(15); System.out.println(stack);
Solution
Step 1: Trace push operations
Elements 5, 10, and 15 are pushed in order, so stack stores them as [5, 10, 15].Step 2: Understand Stack's toString()
Java's Stack prints elements from bottom to top in list format, so output is [5, 10, 15].Final Answer:
[5, 10, 15] -> Option DQuick Check:
Stack print shows bottom to top [OK]
- Assuming print shows top element first
- Confusing stack with queue order
- Expecting reversed list output
Stack<String> stack = new Stack<>();
stack.push("Hello");
stack.push(100);Solution
Step 1: Check stack type and pushed elements
The stack is declared to hold Strings only, but 100 is an integer.Step 2: Identify type mismatch error
Pushing an integer into a String stack causes a compile-time type error.Final Answer:
Cannot push integer 100 into a Stack of Strings -> Option CQuick Check:
Type mismatch error = pushing wrong data type [OK]
- Ignoring generic type mismatch
- Thinking push method missing
- Assuming size needed at init
Solution
Step 1: Understand stack order after pushes
Elements pushed first are at bottom, last pushed at top.Step 2: Match push order to desired stack
To get 3 at bottom, then 7, then 5 on top, push in order: 3, then 7, then 5.Final Answer:
push(3), push(7), push(5) -> Option AQuick Check:
Push order = bottom to top [OK]
- Reversing push order
- Confusing push with pop order
- Assuming stack inserts at bottom
Ready to prove it?
Pass the test and this pattern moves unknown
