Repeat Code With LeetCode — Reverse A String

Or “My Quest To Find The Easiest Two-Pointer Problem On LeetCode”

Evan SooHoo
4 min readMay 2, 2024
Photo by Amie Bell on Unsplash. If I had the money to afford Unsplash+, I would have included an adorable picture of a cat about to reverse some string

When I write about Grokking The Coding Interview (a paid online resource created by a company called DesignGurus), things always get a little bit…odd. The resource is only available for people who pay, which is why I like linking this HackerNoon Article by the founder of Educative. But Educative and DesignGurus are no longer partners…if I am not mistaken, the HackerNoon article I just linked was edited to change all its links to those of other Educative courses.

Anyway, coding pattern #1 for coding interviews is the Two-Pointer Technique. You can read a free LeetCode article about the Two-Pointer technique here and you can find ALL LeetCode two-pointer problems here (there are about 200), but here’s how the HackerNoon article explains it:

Two Pointers is a pattern where two pointers iterate through the data structure in tandem until one or both of the pointers hit a certain condition.Two Pointers is often useful when searching pairs in a sorted array or linked list; for example, when you have to compare each element of an array to its other elements.

Two pointers are needed because with just pointer, you would have to continually loop back through the array to find the answer. This back and forth with a single iterator is inefficient for time and space complexity — a concept referred to as asymptotic analysis. While the brute force or naive solution with 1 pointer would work, it will produce something along the lines of O(n²). In many cases, two pointers can help you find a solution with better space or runtime complexity.

Like many things, this makes a lot more sense with an example…which is why I set out to find an easy LeetCode example with the highest acceptance percentage.

By that metric, this is the easiest Two-Pointer problem in all of LeetCode. Ironically, I think that this problem is harder than the one I am about to cover. I imagine this is because you can solve it with brute force — in fact, if you click on “hint” the hint is that you can just use brute force.

The Problem: Reverse String

Description: Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Difficulty: Easy

Skills Required: Two-Pointer Technique

Fun Facts: Krazam solved this problem, so now it is another meme

The Solution

According to the most upvoted answer on StackOverflow, the simplest way to solve this in C++ is with two lines:

#include <algorithm>
std::reverse(str.begin(), str.end());

…but if your idea of coding is to type a common question into Google, visit a website full of like-minded people, and find a pre-built library implementation that is concise, readable, and eliminates the potential errors you can have by rolling out your own solution, then obviously you have not been spending much time doing coding interviews.

There is a key constraint above: In-place. Had it not been for that constraint, you may have thought to simply create a string, set a pointer to the end of the string, and then go backwards.

By the way, I really wish they called this the Two-Index Technique or something. These pointers are not C or C++ pointers.

The idea here is that you set one pointer to the beginning of the string, one pointer to the end, and then swap characters until you hit the middle.

That’s it.

I have taken a look at other Two-Pointer problems, and most of them are quite a bit more complicated than this. My ulterior motive is to work my way up to this, a LeetCode Two-Pointer question that I actually received in a real interview and failed many years ago.

Come to think of it, I failed another important interview many years before that, and that problem was also a Two-Pointer question.

As for the code above, the only thing I will add is how it swaps. You need a temp character to do a swap…otherwise you would “corrupt” your original. Say you wanted to swap the letters a and b…

ab

so you changed a to b. Fine.

bb

but what do you then set b to? You have to save a copy of the first character.

ba

Closing Thoughts

--

--

Evan SooHoo

A software engineer who writes about software engineering. Shocking, I know.