Reverse a String

This one looks easy. Just flip the string, right? But the interviewer is not asking because it is hard. They want to see how you think. Do you reach for a built-in function and stop there? Or can you show the logic underneath? That is the real test here. They want to know if you understand in-place reversal and the two-pointer trick.

πŸ”„ The Problem

You are given a string. A string is just a sequence of characters, like the letters in a word. You have to return it with the characters in reverse order. If you can, do it in place. In place means you change the same memory. You do not create a brand new string just to hold the answer.

Input: "hello"
Output: "olleh"
Input: "FreeCodingSchool"
Output: "loohcSgnidoCeerF"

🐒 The Simple Approach First

The first idea most people have is simple. Make a new empty string. Then walk through the original from the last character to the first. Add each one to the new string. At the end you have the reversed version.

It works. But there is a catch. You are building a whole new string in memory. So you use extra space equal to the size of the input. In an interview that is fine as a first answer. But they will almost always ask, β€œcan you do it without the extra string?” That is your signal to talk about the better way.

🎯 The Optimal Approach: Two Pointers

Here is the trick the interviewer wants to hear. Put one pointer at the start of the string and one at the end. A pointer here just means a variable holding an index, a position in the string. Now swap the two characters at those positions. Then move the left pointer one step right and the right pointer one step left. Keep going until they meet in the middle.

Think of it like a row of students sitting on a bench. The first student and the last student stand up and switch seats. Then the second and second-to-last switch. You keep going inward. When you reach the middle, everyone has swapped and the row is reversed. No new bench needed. You reused the same one.

One thing to remember. In Java, Python, and JavaScript a string cannot be changed once it is made. We say it is immutable, which means its value is fixed and you cannot edit a single character inside it. So in those languages we first turn the string into a character array or list. We swap inside that. Then we join it back into a string at the end.

Steps to reverse the string

  1. Set left to the first index and right to the last index.
  2. While left is less than right, swap the characters at left and right.
  3. Move left one step forward and right one step backward.
  4. Stop when the two pointers meet or cross. The string is now reversed.
reverse_string.py
# Strings are immutable, so we work on a list of characters.
def reverse_string(s):
chars = list(s)
left = 0
right = len(chars) - 1
while left < right:
# Swap the characters at the two ends.
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
return "".join(chars)
print(reverse_string("hello"))

The output of the above code will be:

olleh

⏱️ Time and Space Complexity

The two-pointer way touches each character once. So it is O(n) time. In C and C++ we swap inside the same string, so there is no real extra space. That is O(1). In Java, Python, and JavaScript we need a char array copy first, so that copy counts as O(n) space.

Approach Time Space
New string (simple) O(n) O(n)
Two pointers (C / C++) O(n) O(1)
Two pointers (Java / Python / JS) O(n) O(n)

Tip

Every language has a built-in shortcut. Python has s[::-1], Java has new StringBuilder(s).reverse(), and JS does s.split("").reverse().join(""). Mention that you know these. But then write the manual two-pointer version anyway, because that is the logic the interviewer actually wants to see.

🧩 Key Takeaways

  • βœ… Use two pointers, one at each end, and swap your way inward until they meet.
  • βœ… The two-pointer scan is O(n) time, and it is O(1) extra space when the language lets you edit the string in place.
  • βœ… In Java, Python, and JavaScript strings are immutable, so convert to a char array or list first, then join back at the end.
  • βœ… Know the built-in shortcuts, but show the manual logic in the interview.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    Why does the two-pointer reversal stop when left is no longer less than right?

    Why: Once the pointers meet or cross, every pair from the outside in has been swapped, so the work is done.

  2. 2

    Why do we convert the string to a char array in Java, Python, and JavaScript?

    Why: In those languages a string cannot be edited once created, so we swap inside a mutable array or list instead.

  3. 3

    What is the time complexity of the two-pointer reversal?

    Why: Each character is visited once as the pointers move toward the middle, giving O(n) time.

  4. 4

    Why might an interviewer not want you to just use a built-in reverse function?

    Why: The question tests whether you understand in-place swapping and the two-pointer pattern, not whether you know a library call.

πŸš€ What’s Next?

Share & Connect