DSA Factory
Free Arrays lessonsArrays · Stage 3 · In-place changes · Step 1

Change every box where it is

Double every value without making a new list. About 5 minutes.

Change the list you were given

Sometimes you're asked not to make a new list, but to change the one you have, like correcting marks on the same answer sheet instead of copying it onto a fresh one. That's called changing it in place. It saves memory, and a lot of interview questions ask for it.

To do it, write each new value straight back into the box it came from.

In place: no new list: each box is updated where it is.
Double in place
6
0
1
1
4
2
2
3
i

Box 0: read 3, double it, write 6 back into the same box.

Move 1 of 4

You need the box number, not just the value

Here's a sneaky trap. A loop like "for each value in the list" hands you a copy of each value. Doubling the copy changes nothing in the list, like writing the corrected mark on a sticky note instead of on the sheet.

To write into a box you need its number. So loop over the box numbers and write through them.

Changing the copy: leaves the list untouched.
Loop over box numbers: so you can write into the box.
In code
for i in range(len(nums)):
    nums[i] = nums[i] * 2
return nums
Quick check

This runs on the list 1, 2. What does the list hold afterwards?

for value in nums: value = value * 2
  1. A1, 2
  2. B2, 4
  3. CAn error
Show the answer

1, 2. Each value is a copy. Doubling the copy doesn't touch the list.

Your problem

Double in place

Double every value in the list, changing the list itself, and return it.

Example
nums = [3, 1, 4, 2] → [6, 2, 8, 4]

0 ≤ n ≤ 1,000 · −1,000 ≤ each value ≤ 1,000

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve