CodePark/ problems / Reverse an Array In Place
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Foundations

Reverse an Array In Place

easyabout 10 minutes in an interview

Write reverseInPlace(arr) that reverses the array arr in place. In place means you change the array you were given. Its first item becomes its last, its second item becomes its second to last, and so on.

  • Return nothing (undefined). The caller sees the change through their own reference to the array.
  • Don't use .reverse(), and don't build a second array. Use only a few extra variables.
  • The array can be empty or hold one item. Items can be any type.
Input: arr = [1, 2, 3, 4, 5] Output: arr is now [5, 4, 3, 2, 1]
Input: arr = ["a", "b"] Output: arr is now ["b", "a"]
Input: arr = [] Output: arr is still []