Back to Benchmark

Stack Push Operation - O(1) Complexity

Understanding how stacks work and why push operations are constant time

What is a Stack?

A stack is a data structure that follows the LIFO (Last In, First Out) principle. Think of it like a stack of plates or books - you can only add or remove items from the top!

Key Operations:

  • Push: Add an element to the top of the stack (O(1))
  • Pop: Remove the top element from the stack (O(1))
  • Peek/Top: Look at the top element without removing it (O(1))

Stacks are implemented using arrays (or linked lists), and since push and pop operations only work at one end (the top), they are extremely efficient - O(1) constant time!

How Does Push Work?

1. Add to the End

When you push an element, it's added to the end of the array. Since arrays support O(1) insertion at the end, push is O(1).

2. No Shifting Required

Unlike inserting at the beginning of an array (which requires shifting all elements), pushing to the end doesn't require any element movement.

3. Simple Array Operation

Push is essentially just:

stack[stack.length] = newElement; // O(1)
stack.push(newElement); // O(1)
Interactive Stack Visualization
Try pushing and popping elements to see how a stack works!
Stack Size: 0 | Top Element: Empty
Stack is empty
↑ Top of Stack

Code Example:

// Creating and using a stack
const stack = [];
// O(1) - Push operation
stack.push(10); // [10]
stack.push(20); // [10, 20]
stack.push(30); // [10, 20, 30]
// O(1) - Pop operation
const top = stack.pop(); // Returns 30, stack: [10, 20]
Built-in Functions for Stacks and Queues
Most programming languages provide built-in functions for stack and queue operations with O(1) push/pop operations:

JavaScript/TypeScript

array.push(value) - O(1) - Add to end (stack)
// Push element onto stack
array.pop() - O(1) - Remove from end (stack)
// Pop element from stack
array.shift() - O(n) - Remove from start (queue)
// Note: O(n) due to element shifting
array.unshift(value) - O(n) - Add to start
// Note: O(n) due to element shifting

Python

list.append(value) - O(1) - Add to end (stack)
// Push element onto stack
list.pop() - O(1) - Remove from end (stack)
// Pop element from stack
collections.deque - O(1) - Efficient queue
// deque.append() and deque.popleft() are both O(1)
queue.Queue - O(1) - Thread-safe queue
// Queue.put() and Queue.get() for thread-safe operations

Java

Stack.push(value) - O(1) - Push to stack
// java.util.Stack: stack.push(value)
Stack.pop() - O(1) - Pop from stack
// Remove and return top element
Stack.peek() - O(1) - View top element
// Return top without removing
Queue.offer(value) - O(1) - Add to queue
// java.util.Queue: queue.offer(value)
Queue.poll() - O(1) - Remove from queue
// Remove and return head element

C++

stack.push(value) - O(1) - Push to stack
// std::stack: stack.push(value)
stack.pop() - O(1) - Pop from stack
// Remove top element (doesn't return value)
stack.top() - O(1) - View top element
// Return reference to top element
queue.push(value) - O(1) - Add to queue
// std::queue: queue.push(value)
queue.pop() - O(1) - Remove from queue
// Remove front element
queue.front() - O(1) - View front element
// Return reference to front element
Why is Stack Push O(1)?

1. Array End Insertion

Adding an element to the end of an array is O(1) because you know exactly where the end is (array.length), and you don't need to move any existing elements.

2. No Element Shifting

Unlike inserting at the beginning or middle of an array (which requires shifting all subsequent elements), pushing to the end requires no shifting at all.

3. Single Operation

Push is a single, atomic operation:

stack[stack.length] = value; // One assignment
// or stack.push(value) - same thing!

⚠️ Note: Dynamic Array Resizing

When an array needs to grow beyond its capacity, it may need to be resized (allocate new memory and copy elements). However, this happens infrequently and is amortized O(1) - meaning the average cost per push operation is still O(1).

Time Complexity Comparison
OperationStackArray (Beginning)Explanation
Push (Add to End)O(1)O(1)Both are O(1) - adding to the end requires no shifting
Pop (Remove from End)O(1)O(1)Both are O(1) - removing from the end requires no shifting
Insert at BeginningN/AO(n)Arrays require shifting all elements - stacks don't support this
Access by IndexN/AO(1)Arrays support random access - stacks only allow top access
Real-World Examples

📚 Browser History

When you click "Back" in your browser, it pops from a stack of visited pages. The most recent page (last in) is the first one you go back to (first out)!

🧮 Expression Evaluation

Stacks are used to evaluate mathematical expressions and convert between infix, prefix, and postfix notation. Each operator and operand is pushed/popped as needed.

🔙 Undo/Redo Functionality

Text editors use stacks to implement undo. Each action is pushed onto a stack, and undo pops the most recent action. Redo uses another stack!

📞 Function Call Stack

When functions call other functions, the computer uses a call stack. Each function call is pushed, and when it returns, it's popped. This is why recursion works!

🎮 Game State Management

Games use stacks to manage game states (menu, gameplay, pause, etc.). Pushing a new state pauses the current one, and popping returns to the previous state.

Key Takeaways
  • Stack push and pop are O(1) operations

    Both operations work at one end only, requiring no element shifting

  • Stacks follow LIFO principle

    Last In, First Out - the most recently added element is the first one removed

  • Perfect for certain problems

    Stacks are ideal for problems involving reversal, matching parentheses, and backtracking

  • Simple but powerful data structure

    Despite its simplicity, stacks are fundamental to many algorithms and system operations

Practice Exercises
Test your understanding of stacks with these exercises. Try to solve them yourself before checking the answers!