Bitpath

Boss lesson · Unit 02

Reverse in place

Two indexes, swaps, and no extra memory

12 exercisesabout 3 min

What you practise

  • Aliasing vs copying, slices copy, two-index swap, O(1) extra space

Try a question from this lesson

Pick one

The append goes through b. What does this print?

a = [1, 2, 3]
b = a
b.append(4)
print(len(a))
const a = [1, 2, 3];
const b = a;
b.push(4);
console.log(a.length);
const a = [1, 2, 3];
const b = a;
b.push(4);
console.log(a.length);

The idea

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Alias or copy?

Assignment gives the same array a second name. For an independent array you must copy it, and copying n items costs O(n) time and O(n) memory.

a = [1, 2, 3]
alias = a        # same list
copy = a[:]      # new list, O(n)
copy.append(4)
print(len(alias), len(copy))
const a = [1, 2, 3];
const alias = a;     // same array
const copy = [...a]; // new array, O(n)
copy.push(4);
console.log(alias.length, copy.length);
const a = [1, 2, 3];
const alias = a;     // same array
const copy = [...a]; // new array, O(n)
copy.push(4);
console.log(alias.length, copy.length);

Python notea[:], a.copy() and list(a) all make a new list with the same items.

JavaScript note[...a] spreads the items into a new array. a.slice() does the same.

TypeScript note[...a] spreads the items into a new array and keeps the type: copy is a number[] too.

10 more exercises in the app

  • 2× true or false
  • 2× type the answer
  • 1× order the lines
  • 1× fill the blanks
  • 3× multiple choice
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy