Push Operation on Stack in DSA C - Time & Space Complexity
We want to understand how the time needed to add an item to a stack changes as the stack grows.
How does the push operation behave when the stack size increases?
Analyze the time complexity of the following code snippet.
// Push operation on stack
void push(int stack[], int *top, int value, int max_size) {
if (*top == max_size - 1) {
// Stack overflow
return;
}
(*top)++;
stack[*top] = value;
}
This code adds a new value to the top of the stack if there is space.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single assignment to the stack array at the top position.
- How many times: Exactly once per push call, no loops or recursion involved.
Each push does a fixed number of steps regardless of stack size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (check, increment, assign) |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The number of steps stays the same no matter how big the stack is.
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: "Push takes longer as the stack grows because it has to move all items."
[OK] Correct: Push only adds one item at the top without moving others, so time does not grow with stack size.
Knowing push is always quick helps you explain why stacks are efficient for adding data, a key skill in many coding problems.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of push change?"
Practice
push operation do in a stack?Solution
Step 1: Understand stack operations
A stack is a data structure where elements are added or removed from the top only.Step 2: Define push operation
The push operation adds a new element on top of the existing elements in the stack.Final Answer:
Adds an element to the top of the stack -> Option BQuick Check:
Push means add on top = C [OK]
- Confusing push with pop operation
- Thinking push removes elements
- Mixing stack with queue operations
5 onto a stack named s in C?Solution
Step 1: Understand typical C stack push syntax
In C, stack operations are usually functions where the stack is passed as the first argument.Step 2: Match syntax with parameters
The correct function call to push 5 onto stack s ispush(s, 5);.Final Answer:
push(s, 5); -> Option DQuick Check:
Function push(stack, value) = A [OK]
- Using object.method syntax (not C style)
- Swapping argument order
- Assigning value instead of calling function
int stack[5];
int top = -1;
// Push function
void push(int val) {
if(top < 4) {
top++;
stack[top] = val;
}
}
push(10);
push(20);
push(30);Solution
Step 1: Trace each push call
Initially, top = -1. After push(10), top=0, stack[0]=10. After push(20), top=1, stack[1]=20. After push(30), top=2, stack[2]=30.Step 2: List stack content from bottom to top
Stack elements from index 0 to top (2) are [10, 20, 30].Final Answer:
[10, 20, 30] -> Option AQuick Check:
Push adds at top increasing index = B [OK]
- Reversing order of elements
- Confusing top index with element values
- Assuming stack is empty after pushes
int stack[3];
int top = 0;
void push(int val) {
if(top <= 3) {
stack[top] = val;
top++;
}
}Solution
Step 1: Analyze top and condition
top starts at 0, which is valid for empty stack at index 0. The condition allows top to be 3, which is out of bounds for stack[3] (indices 0 to 2).Step 2: Correct the condition
Change condition totop < 3to prevent overflow and invalid access.Final Answer:
Condition should be top < 3, not top <= 3 -> Option AQuick Check:
Stack max index 2 means top < 3 [OK]
- Allowing top to equal array size causing overflow
- Starting top at -1 but not adjusting code
- Incrementing top before assignment causing skip
top = 2 currently, what will happen if we try to push two more elements without popping?Choose the correct behavior.
Solution
Step 1: Understand current stack state
Array size is 4, so valid indices are 0 to 3. top=2 means top element is at index 2, so one slot remains at index 3.Step 2: Push two elements
First push increments top to 3 and stores element at index 3 (valid). Second push tries to increment top to 4, which is out of bounds, causing overflow.Final Answer:
First push succeeds, second push causes stack overflow -> Option CQuick Check:
Array size 4, top max index 3, overflow after that [OK]
- Assuming stack auto-expands
- Thinking both pushes fail immediately
- Confusing top index with count
Ready to prove it?
Pass the test and this pattern moves unknown
