Magicsheet logo

Reverse String

Easy
51.7%
Updated 6/1/2025

Reverse String

What is this problem about?

The Reverse String interview question asks you to reverse an array of characters in-place. You may not allocate extra space for another array — the modification must happen within the input array using O(1) extra memory. This is one of the most fundamental string/array manipulation problems in programming interviews.

Why is this asked in interviews?

This problem is asked at Apple, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, and many more companies as an entry-level screen. It directly tests the two-pointer technique — the simplest and most widely applicable pattern in array manipulation. Despite its simplicity, it is the foundation for palindrome checking, string anagram analysis, and more complex reversal-based problems. Candidates who solve it cleanly and explain their reasoning set a strong tone for the interview.

Algorithmic pattern used

The pattern is the two-pointer technique. Place left at index 0 and right at index len(s) - 1. While left < right: swap s[left] and s[right], then increment left and decrement right. This achieves O(n) time and O(1) space. In Python, the elegant one-liner is s.reverse() or s[:] = s[::-1], but the two-pointer approach demonstrates the underlying logic explicitly.

Example explanation

Array: ['h','e','l','l','o']

  • left=0, right=4: swap 'h' and 'o' → ['o','e','l','l','h'].
  • left=1, right=3: swap 'e' and 'l' → ['o','l','l','e','h'].
  • left=2, right=2: left == right → stop.

Result: ['o','l','l','e','h'].

Array: ['a','b']:

  • Swap 'a' and 'b'. Result: ['b','a'].

Common mistakes candidates make

  • Using a temporary variable for swapping — in Python, tuple unpacking (s[left], s[right] = s[right], s[left]) is cleaner.
  • Allocating a new reversed list instead of modifying in-place.
  • Using while left <= right (should be strict <), causing an unnecessary no-op swap when left == right.
  • Treating a string as a character array directly in languages where strings are immutable.

Interview preparation tip

For the Reverse String coding problem, the two-pointer string interview pattern is fundamental. Even though Python has s.reverse(), always demonstrate the two-pointer implementation — it shows you understand the algorithm, not just the library call. Interviewers at Goldman Sachs and Capgemini use this as the first question to establish your comfort with basic pointer manipulation. Solve it in under 2 minutes and immediately offer to discuss variations like "reverse only vowels" or "reverse words in a string."

Similar Questions