Reverse String — Two Pointer In-Place Swap at Amazon and Apple
Advertisement
Problem Statement
Write a function that reverses a string. The input is given as an array of characters s and you must modify the array in place using O(1) extra memory.
Constraints:
1 <= s.length <= 10^5s[i]is a printable ASCII character
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]Why This Problem Matters
LeetCode 344 Reverse String is the cleanest demonstration of the opposite-end two pointer swap pattern. Amazon uses it to verify that candidates can write a constraint-respecting in-place algorithm without reaching for arr.reverse(). Apple asks it as a 5 minute opener before moving to a harder follow-up. Meta uses it as a sanity check before a system design round.
The problem looks trivial because most languages provide a built-in reverse. The interview value is in showing that you can write the in-place version yourself, that you understand why the loop terminates at the midpoint, and that you handle even-length and odd-length strings correctly. Failing this question kills a candidacy faster than failing a hard problem.
The pattern generalizes directly to LC 345 Reverse Vowels, LC 541 Reverse String II, and LC 151 Reverse Words in a String. Master the two pointer swap here and these variants take a minute each.
The Core Insight
Reversing a sequence is symmetric: the first element trades with the last, the second trades with the second-to-last, and so on. Two pointers starting at opposite ends and moving inward perform exactly that pairing.
The loop terminates when the pointers meet or cross. For even-length strings they cross without meeting; for odd-length strings they meet at the middle element which trades with itself, a no-op that is harmless. Either way, exactly n / 2 swaps are performed.
There is no shrinking, no expanding, no auxiliary state. This is the purest two pointer template and it runs in O(n) time and O(1) space.
Visual Dry Run
For input s = ["h","e","l","l","o"]:
| Step | Left | Right | Before Swap | After Swap |
|---|---|---|---|---|
| 1 | 0 | 4 | h e l l o | o e l l h |
| 2 | 1 | 3 | o e l l h | o l l e h |
| 3 | 2 | 2 | o l l e h | pointers meet, exit |
Solution (Optimal)
class Solution:
def reverseString(self, s: list[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1var reverseString = function(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
};Time: O(n) — exactly n / 2 swaps Space: O(1) — pointers and a temp variable for the swap
Common Mistakes
- Using
s = s[::-1]in Python, which creates a new list and violates the in-place constraint - Looping
for i in range(len(s))and swapping every index, which reverses then reverses again - Off-by-one when initializing
right = len(s)instead oflen(s) - 1 - Using
<=instead of<in the loop condition, doing one extra harmless swap on the middle element - Returning
sfrom a function whose signature returnsNoneorvoid
Interview Tips
- Confirm out loud that the modification is in place and you cannot allocate a new array
- Walk through both an even-length and an odd-length example on the whiteboard
- After coding, articulate why the loop terminates: "Each iteration moves the pointers one step closer, so after n / 2 iterations they meet or cross"
- Offer LC 345 Reverse Vowels as a natural follow-up
Follow-up Questions
- How would you reverse only the vowels in the string? (Hint: LC 345, skip non-vowels with two pointers)
- What if you must reverse words separated by spaces? (Hint: LC 151, reverse whole string then reverse each word)
- How would you reverse a string in chunks of size k? (Hint: LC 541, two pointers per chunk)
- Can this be done recursively? (Hint: yes, but stack space is O(n), defeating the constraint)
- How does this generalize to a singly linked list? (Hint: LC 206, three-pointer iterative reverse)
Key Takeaways
- LeetCode 344 Reverse String is the canonical opposite-end two pointer swap problem
- Optimal solution uses two pointers walking inward with exactly n / 2 swaps
- O(n) time and O(1) space are required by the in-place constraint
- The loop condition
left < righthandles both even and odd lengths correctly - The pattern generalizes to LC 345, LC 541, LC 151, and any in-place sequence reversal
- Built-in reverse functions are forbidden in interviews even when the language provides them
- Amazon, Apple, and Meta all use this as a 5 minute warm-up before harder questions
Advertisement