Algorithms and Data Structures · Junior · Question 10 of 75

What is the space complexity of this function?

function double(arr) {
  return arr.map((x) => x * 2);
}

Explanation

map creates a new array of the same length, so the extra memory grows linearly with n. Space complexity counts additional memory, not the input itself.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 10

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz