Flip in place
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
Reverse the list items in place: change the list itself so its items are in the opposite order, then return it.
In place means without making a second list. Only use a few extra variables, however long the list is.
Examples
Example 1
- Input
- items = [1, 2, 3, 4, 5]
- Output
- [5, 4, 3, 2, 1]
With an odd number of items, the middle one stays where it is.
Example 2
- Input
- items = ["h", "e", "y"]
- Output
- ["y", "e", "h"]
Example 3
- Input
- items = []
- Output
- []
Nothing to flip.
Rules
itemshas between 0 and 100,000 items, of any kind.- Don't use the built-in
reverse(): the point is to see how it works.
Words on this page, in plain English (3)Open if any word is new to you
- Variable
- A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
- Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
- In place
- Changing the list you were given, instead of making a new one. Saves memory.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We must reverse the list itself, not build a new reversed copy. Then we return that same list. "In place" problems are common in interviews because they test whether you can rearrange data without extra memory.
2. Solve an example by hand
With [1, 2, 3, 4, 5]: the 1 belongs where the 5 is, and the 5 where the 1 is. Swap them: [5, 2, 3, 4, 1]. Now the 2 and the 4: [5, 4, 3, 2, 1]. The 3 is in the middle and stays. Done after just two swaps.
3. The simple way
Build a new list by reading from the back:
function flipInPlace(items) { const flipped = []; for (let i = items.length - 1; i >= 0; i--) flipped.push(items[i]); return flipped;}This returns the right order, but it isn't in place: it uses a whole second list, O(n) memory, and the original list is left unchanged.
4. Spot the pattern
Pairs of positions swap with each other: first with last, second with second-last. That's two pointers from both ends.
5. The two-pointer idea
Start left at the first position and right at the last. Swap the two items, then move both pointers one step inward. Stop when they meet or cross: everything has been swapped.
Swap from the outside in
items
function flipInPlace(items) { let left = 0; let right = items.length - 1; while (left < right) { const saved = items[left]; items[left] = items[right]; items[right] = saved; left++; right--; } return items;}Pointers at both ends.
Swapping needs a temporary variable. If you write items[left] = items[right] first, the old value at left is gone. So save it first, then overwrite, then put the saved value on the other side.
6. The code, line by line
let left = 0; let right = items.length - 1;Pointers at both ends.
while (left < right) {Keep swapping while
leftis beforeright. When they meet, the middle item is already where it belongs.const saved = items[left]; items[left] = items[right]; items[right] = saved;The swap.
savedkeeps the left item safe while we overwrite it.left++; right--;Both pointers step inward.
return items;Return the same list we were given, now reversed.
JavaScript also has a shortcut for swapping: [items[left], items[right]] = [items[right], items[left]];. It does the same thing; the saved version shows what's happening.
7. How fast is it?
| New list | Swap in place | |
|---|---|---|
| Time | O(n) | O(n): n ÷ 2 swaps |
| Extra memory | O(n) | O(1): three variables |
Common mistakes
- Losing a value while swapping. Always save one value before you overwrite it.
- Looping over the whole list. If you swap for every position, the second half swaps everything back, and you end where you started.
- Returning a new list. It might look right, but the original list hasn't changed.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Flip in place
Read the 12 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function flipInPlace(items) { let left = 0; let right = items.length - 1; while (left < right) { const saved = items[left]; items[left] = items[right]; items[right] = saved; left++; right--; } return items;}