Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Base case: empty or single-char string returns itself. Recursive case: `reverse(s[1:]) + s[0]`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can reverse recursively and reason about its cost.
Where candidates slip
Wrong placement of the head, or missing base case.
How to stand out
Note the O(n) in-place two-pointer swap as the efficient alternative.
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
Reverse-rest + prepend-first
1Hint 1Explore hintHide hint
Reverse the tail, then where does the first character go?
2Hint 2Explore hintHide hint
Base case: a 0- or 1-length string.
3Hint 3Explore hintHide hint
Why is the naive concatenation version O(n²)?
A · Core idea
Reverse by recursion: reverse everything after the first character, then put the first character at the end. Base case is a string of length ≤ 1, which is its own reverse.
Why append the head at the end. reverse(s[1:]) + s[0] reverses the tail and tacks the original first character on after it — which places it last, exactly where a reversal sends it. Prepending instead would not reverse anything.
Why this naive version is O(n²). Each call builds a new string by concatenation, copying O(n) characters, across n calls → O(n²) time plus O(n) stack. The efficient form swaps characters in a char array with two indices moving inward (O(n) time, in place) — mention it as the upgrade.
Recursion here is a teaching device; iteration (two-pointer swap) is what you'd ship.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "abc""abc"
reverse("bc") + 'a'
waits"cba"C · Code with teaching comments
D · Why it works and complexityO(n²) (string concat) / O(n) with chars timeO(n) stack spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n²) (string concat) / O(n) with chars time, O(n) stack space — n recursive calls each concatenating O(n) characters → O(n²); the char-swap form is O(n).
E · Edge cases and pitfalls4s = "" — Base case → "".Explore edge casesHide edge cases
Edge cases handled
s = ""Base case → "".s = "x"Single char → "x".
Pitfalls
- Forgetting the base case.
- Appending the first char at the front instead of the end.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String reverse(String s) { if (s.length() <= 1) return s; return reverse(s.substring(1)) + s.charAt(0); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Forgetting the base case.
- Appending the first char at the front instead of the end.
Revise
Carry the pattern into the interview
Pattern to remember
Reverse-rest + append-first
Code pattern to memorize
reverse(s) = len<=1 ? s : reverse(s[1:]) + s[0]Rules to remember
- 1len ≤ 1 → return s.
- 2reverse(tail)+head.
- 3Char swap for O(n).
Use this pattern when
- Recursion practice on strings
Choose another pattern when
- Performance matters — swap in place
Interview takeaway
String recursion: solve the tail, then attach the head.
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: Reverse-rest + append-first. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups