How to reverse a string in O1 complexity runtime?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reversing a string in true O(1) runtime is not possible when you must produce an actual reversed copy. Any real reversal must touch characters, so runtime grows with input length. The practical target is O(n) time with predictable memory behavior, or an O(1) view object when lazily indexing original data.
Why Full Reversal Cannot Be O(1)
A string of length n contains n positions that may need to move. If output must contain all characters in reverse order, each character must be read and written at least once. That gives a lower bound of linear work.
In short:
- Reading all characters requires
O(n) - Writing reversed output requires
O(n) - Therefore full materialization cannot be constant time
Efficient In-Place Style on Mutable Buffers
If your language supports mutable arrays, a two-pointer swap loop is optimal in practice.
Complexity is O(n) time and O(1) extra space.
Immutable String Languages
When strings are immutable, create a new reversed string. Python slicing is concise and implemented efficiently in C.
This is still O(n) time and O(n) memory for the new string.
C++ Example with Standard Library
std::reverse is a reliable option for mutable character containers.
The algorithm is linear and highly optimized by standard library implementations.
The O(1) Case People Usually Mean
You can create a lightweight reversed view in O(1) time by storing original data and remapping indexes. This does not materialize a new string immediately.
Constructing ReversedView is O(1), but iterating across all characters is still O(n) total.
Choosing the Right Approach
Pick by requirement:
- Need actual reversed string now: do standard linear reversal
- Need only occasional reverse-index access: use a view abstraction
- Need minimal allocations on large data: mutate buffers in place when possible
The best solution depends on output contract, not on forcing O(1) where it cannot apply.
Interview Friendly Explanation
If this question comes up in an interview, answer in two parts. First, state clearly that full reversal is linear because every character must be processed. Second, mention that O(1) construction is possible only for a lazy reverse view that defers work until access time. This shows you understand both asymptotic limits and practical design tradeoffs.
Common Pitfalls
- Claiming
O(1)for algorithms that still read all characters - Ignoring immutability costs in languages that allocate new strings
- Benchmarking tiny inputs and drawing wrong complexity conclusions
- Forgetting Unicode grapheme boundaries in human-language text reversal
- Optimizing for asymptotic notation while ignoring real memory behavior
Correct complexity analysis prevents misleading performance expectations.
Summary
- Full string reversal cannot be true
O(1)in general. - Materialized reversal requires linear work in input length.
- In-place swaps give
O(n)time with constant extra memory on mutable buffers. - Immutable strings require new allocation for reversed output.
O(1)creation is possible only for lazy reversed views, not full output.

