Stacks — Visual Learning

Reverse String Using Stack —
LIFO makes reversal trivial.

Reversing a string with a stack is the simplest demonstration of LIFO in action. Push every character onto the stack, then pop them all off — because the last character pushed is the first popped, the string comes out reversed. Watch each character push in and pop out on LearnBug and LIFO becomes completely intuitive.

O(n)Time complexity
O(n)Space complexity
PythonLanguage

Why use a stack to reverse?

A stack's LIFO property naturally reverses the order of whatever you push onto it. Push characters in left-to-right order, pop them and the last character comes out first — giving you right-to-left order. This is the same principle used internally by DFS, backtracking algorithms, and undo operations. Understanding reversal with a stack builds the intuition for all of these.

Why visualization changes everything

Reading the code, it's easy to accept that "pop reverses push order" without really seeing why. On LearnBug, you watch the characters stack up like a physical pile, then come off in reverse — the visual makes LIFO feel obvious rather than abstract. Once you see it, every stack-based algorithm makes more sense.

YouTube — Reverse String with Stack
📺 Drop your YouTube embed here
LearnBug — characters stacking and unstacking
🖼 Add a LearnBug screenshot here
Walkthrough

Reversing "hello" step by step

1

Phase 1 — Push all characters

Iterate left to right. Push each character onto the stack. 'h' goes in first, 'o' goes in last.

h
e
l
l
o
Stack (top → bottom): [ o, l, l, e, h ]
2

Phase 2 — Pop all characters

'o' pops first (last in, first out), then 'l', 'l', 'e', 'h'. Each pop appends to the result string.

o
l
l
e
h

Result: "olleh"

3

Why this works — LIFO property

The stack reversed the order automatically. The last character pushed ('o') was the first popped. No swapping, no index arithmetic — just push then pop. This is why stacks are used for reversal, undo, and backtracking.

h
e
l
l
o
o
l
l
e
h
Python Code

Three ways to reverse — stack, slice, and built-in

PythonStack-based reversal — explicit LIFO
def reverse_with_stack(s):
    stack = []

    for char in s:
        stack.append(char)   # push

    result = []
    while stack:
        result.append(stack.pop())  # pop — LIFO reverses order

    return ''.join(result)

print(reverse_with_stack("hello"))  # → "olleh"
PythonPythonic alternatives (for comparison)
s = "hello"

# Slice — most Pythonic
print(s[::-1])              # → "olleh"

# reversed() + join
print(''.join(reversed(s)))  # → "olleh"

# list + reverse in place
chars = list(s)
chars.reverse()
print(''.join(chars))        # → "olleh"

Time & Space Complexity

Time — StackO(n)n pushes + n pops — each character touched twice
Space — StackO(n)Stack holds all n characters simultaneously
Time — SliceO(n)Creates a new string of n characters — same complexity
Why use stack?Learning LIFOIn practice use s[::-1]. Stack version teaches the principle used everywhere else

Frequently asked questions

Why not just use s[::-1] in Python?

You should — s[::-1] is the Pythonic, efficient way to reverse a string. The stack approach is taught because it makes LIFO behaviour explicit and builds intuition for cases where you can't just reverse a slice: reversing words in a sentence, reversing linked list nodes, or undoing operations one step at a time.

What is LIFO and why does it reverse order?

LIFO stands for Last In, First Out. The last item pushed is always the first popped. If you push A, B, C in that order, popping gives C, B, A — the reverse. This is the core property that makes stacks useful for reversal, backtracking, and maintaining history (undo).

How is this used in real algorithms?

DFS uses a stack to reverse the exploration order — diving deep before backtracking. Iterative tree traversal uses a stack to simulate the call stack's LIFO behaviour. Browser history (back button) is a stack. Undo/redo in editors are two stacks. The reversal property of stacks is everywhere in software.

Can I reverse a string in O(1) space?

For strings (immutable in Python): no — you must create a new string, which is O(n) space. For character arrays (mutable): yes — use two pointers swapping from both ends inward. This is the in-place reversal used in the Rotate Array trick and is O(1) extra space.

What interview problems use this concept?

Valid Parentheses (LC 20), Reverse Words in a String (LC 151 — split + reverse the list), Decode String (LC 394 — nested reversal with a stack), and Daily Temperatures (LC 739 — monotonic stack). The core insight — stack reverses order — underlies all of these.

Watch characters push and pop on your own string

Paste your string into LearnBug and see every push and pop as the stack reverses it step by step.

Open Playground →