In place reversal in Javascript

Rachel Cross
3 min readMay 15, 2021

There are many different ways to reverse a string. The most obvious is to split the string into an array and use the .reverse() method. A common interview question is to perform simple operations like this on a string without built in methods. Additionally, you’ll often be asked if you can do these operations in place. In this context, “in-place” means updating the original string rather than creating a new one, or, more specifically as Wikipedia puts it:

in-place means that the algorithm does not use extra space for manipulating the input but may require a small though nonconstant extra space for its operation.

In some languages (Javascript included), you technically can’t accomplish a string reversal in place because strings in Javascript are immutable (i.e. once the string object is assigned to string reference the object value cannot be changed), however you can split the string into an array and do it in place that way. Be sure to specify this in an interview if asked. If you’re asked to simply reverse an array (a very similar process), this can be done in place (truly in place).

This may seem like a basic operation, but understanding simpler processes like this can help you have a broader and deeper vocabulary for tackling more complicated algorithms down the road. In particular, practicing doing simpler algorithms like this in place can be good practice for answering these sorts of follow up questions in an interview.

Here are an explanation of the steps, and followed by that, the code implementing them:

  • To start, we split our string into an array
  • Then we will be using a for loop to loop through the string and switch the order of certain characters
  • For each iteration of our loop (i.e. each time we increment our counter, i), we will be moving from the outside in and swapping each character as we go (i.e. in the first iteration, we’ll be swapping the first and last character, in the second iteration we’ll be swapping the second and second to last character etc.).
  • Since we are moving from the outside in on each end of our array and swapping 2 characters at a time, the condition for our for loop will be while i < string.length / 2 (rather than simply string.length).
  • In order to make the swap in place within our loop, we’ll start out by using a temp variable to store the value of our array at index i. So in the first iteration, we’ll be storing the first item in our array or array[0]. I’ll be referring to array[i] value as the “left” value in our loop, and the value moving from the end of the array to the middle as the “right” value. (Note: even though we’re allocating a separate temp variable as part of our operation, this is still considered an in place algorithm because as mentioned in the above definition from Wikipedia, an in place algorithm may require a small though nonconstant extra space for its operation).
  • We store the left value in a temp variable so that we can make the swap by overwriting each character at the appropriate index directly (you can do it the other way around and store your right value in the temp variable as well, just do the below operations in reverse).
  • After we set the temp variable to left value (array[i]), we then directly reassign the value of the left value to the value of the right value. At this stage in the loop, we have the left value allocated to a temp variable, and both the left and right values are equal to the initial right value.
  • We dynamically get the last item in the array by looking at the index of array.length minus 1, minus i. We do this because array.length minus 1 will give us the last item in the array (since arrays start at 0 index), and decrementing that index by i each time (as i increments), will decrement the index of the right value each time, therefore moving one character to the left on each loop.
  • After we’ve reassigned the first value, we simply reassign the right value to the left value stored in our temp variable.
  • Once our loop is completed, we rejoin the string and we’re done!

Code below:

Hope this helps!

--

--