Virtual Algorithm Lab

Reverse a String: Out-of-Place

{ }
++ ++
0
0
new_s += s[i] — O(N) memory alloc
Out-of-Place Allocation System
drag to rotate · pinch to zoom · 2-finger drag to pan
L
R

Out-of-Place Reversal

"hello" → "olleh" · iterate backwards to build a new string

The Word, Live
Custom Word:
Step by Step
Step 1 / 9

The Code Behind It
reverse_string.py

    
In plain English:
Theory & Fundamentals

String Immutability & O(N) Space

In languages like Python and Java, strings are immutable (they cannot be changed in-place). To reverse a string, we must allocate a completely new string in memory and build it character-by-character from the end of the original string. This requires O(N) auxiliary space.

Knowledge Check

Q1: Memory Allocation

If the original string has 10 characters, how many new character slots must be allocated in memory?

Q2: Iteration Direction

To build the reversed string sequentially, where does the loop pointer start?

Q3: Space Complexity

What is the auxiliary space complexity of reversing an immutable string by building a new one?