Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Convert to a char array and two-pointer swap the ends inward, or in Python use slicing `s[::-1]`. O(n) time.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know strings are immutable and reverse in place on a char array.
Where candidates slip
Mutating the String, or O(n²) prepend-building.
How to stand out
Mention Unicode/surrogate pairs as a caveat for true character reversal.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Two-pointer swap
1Hint 1Explore hintHide hint
Reuse the array-reverse idea — on characters this time.
2Hint 2Explore hintHide hint
Java strings are immutable: work on a char array.
3Hint 3Explore hintHide hint
Two pointers from both ends, swap, converge; Python can use s[::-1].
A · Core idea
Reversing a string is the array two-pointer swap applied to characters: swap the first and last, second and second-last, converging in the middle. O(n) time, O(1) extra space.
Why a char array in Java. Java Strings are immutable — you can't assign s[i] = …. So you convert to a char[], swap in place, and build a new String. Python strings are also immutable, but slicing s[::-1] builds the reversed copy in one expression.
Why two pointers, not a fresh buffer per char. The in-place swap touches each character once with no per-character allocation. Building the result by repeatedly prepending characters would be O(n²) in many languages.
Loop bound. Use left < right so the middle character of an odd-length string is left untouched and the pointers don't cross and undo the work.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "abcd"l=0,r=3
swap a,d
dbca"dcba"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) extra (in place on char array) spaceOpen the invariant and proofExplore the proofHide the proof
Mentioning that Java strings are immutable (hence the char array) shows language awareness.
ComplexityO(n) time, O(1) extra (in place on char array) space — n/2 swaps with O(1) extra state (the output buffer aside).
E · Edge cases and pitfalls5s = "a" — Single char unchanged.Explore edge casesHide edge cases
Edge cases handled
s = "a"Single char unchanged.s = ""Empty string unchanged.s = "ab"Two chars swap to "ba".
Pitfalls
- Trying to mutate a Java String directly (immutable).
- Off-by-one on the right index (length vs length − 1).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String reverse(String s) { char[] c = s.toCharArray(); int left = 0, right = c.length - 1; while (left < right) { char t = c[left];Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Trying to mutate a Java String directly (it's immutable).
- Off-by-one on the right index.
Revise
Carry the pattern into the interview
Pattern to remember
Two-pointer swap (chars)
Code pattern to memorize
char[] c; l,r=0,n-1; while l<r: swap; l++; r--Rules to remember
- 1Work on a char array in Java.
- 2Swap ends, converge.
- 3Python: s[::-1].
Use this pattern when
- In-place reversal of a sequence
Interview takeaway
String reverse is array reverse on characters.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Two-pointer swap (chars). Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups