Skip to content
C Codeloom
DSA

Reverse String — Two-Pointer In-Place Solution

Solve the Reverse String problem in place using the two-pointer technique. Walkthrough, complexity analysis, edge cases, and clean Java, Python, and C++ implementations.

·4 min read · By Codeloom
Beginner 7 min read

What you'll learn

  • Read and rephrase the problem statement
  • Apply the two-pointer pattern to reverse an array in place
  • Implement the solution in Python, Java, and C++
  • Reason about time and space complexity
  • Handle edge cases like empty input and single elements

Prerequisites

  • Basic arrays and loops

Reverse String is one of the most popular warm-up questions for a reason: it teaches the two-pointer pattern, in-place mutation, and a small amount of complexity analysis — all in under ten lines of code.

The Problem

Write a function that reverses a string. The input is given as an array of characters s. You must do this by modifying the input array in place with O(1) extra memory.

Example: s = ["h","e","l","l","o"] becomes s = ["o","l","l","e","h"].

The constraint that you cannot allocate a new array rules out the obvious “create a new reversed array” solution.

Intuition

Reversing an array is a swap problem. The character at index 0 should swap with the character at the last index, index 1 with the second-to-last, and so on, until the pointers meet in the middle. This naturally maps to two indices: left starting at 0 and right starting at n - 1. On each iteration, swap, then move both pointers one step closer to the center. The strict left < right termination keeps the middle element of odd-length arrays untouched (since swapping it with itself is wasted work).

Explanation with Example

Consider s = ["a","b","c","d","e"]. Pointers start at left = 0 and right = 4.

  • Step 1: swap s[0] and s[4]. Array becomes ["e","b","c","d","a"]. Move pointers to 1 and 3.
  • Step 2: swap s[1] and s[3]. Array becomes ["e","d","c","b","a"]. Move pointers to 2 and 2.
  • Loop exits because left is no longer strictly less than right.

The middle element in odd-length arrays stays in place, which is exactly what we want.

start    [ a   b   c   d   e ]
         L               R

swap     [ e   b   c   d   a ]
         ^               ^

step 1       L           R
       [ e   b   c   d   a ]

swap     [ e   d   c   b   a ]
             ^       ^

step 2           L   R
                meet -> stop

result   [ e   d   c   b   a ]
Two pointers walk inward, swapping until they meet

Code

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 -= 1
class Solution {
    public void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            char tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
            left++;
            right--;
        }
    }
}
class Solution {
public:
    void reverseString(vector<char>& s) {
        int left = 0, right = (int)s.size() - 1;
        while (left < right) {
            swap(s[left], s[right]);
            ++left;
            --right;
        }
    }
};

Edge Cases

  • Empty array. left = 0, right = -1, the loop never runs. Correct behavior.
  • Single character. left = 0, right = 0, loop never runs. Correct.
  • Two characters. One swap, then left = 1, right = 0, loop exits.

Using strict inequality left < right makes all of these “just work” with no special cases.

Complexity Analysis

Time complexity is O(n) where n is the length of the array. We touch each index at most once, performing constant-time work per step.

Space complexity is O(1). We use a fixed number of integer pointers and at most one temporary character. We never allocate a new array.

How to Explain It in an Interview

Lead with the swap symmetry: index i swaps with n - 1 - i. Translate that into two pointers moving inward. Highlight the strict < termination, and call out the empty/single-character cases that fall out of it. Mention common mistakes: writing left <= right (does redundant work) or for i in range(n) with a swap (double-reverses back to the original).

  • Valid Palindrome
  • Reverse Words in a String II
  • Container With Most Water

Wrap up

Reverse String is a great five-minute drill for the two-pointer pattern. Memorize the shape: two indices moving inward, a swap inside the loop, and strict less-than as the termination condition. The same shape powers harder problems so the time you invest here pays compound interest.