arrayMethodBigO Flashcards

1
Q

What’s the time and space complexity of arr.push()? Explain your reasoning.

A

Time complexity: O(1)
Space complexity: O(1)
Justification: Adding to the end of an array requires no shifting and happens in place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the time and space complexity of arr.pop()? Explain your reasoning.

A

Time complexity: O(1)
Space complexity: O(1)
Justification: Removing from the end of an array requires no shifting and happens in place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the time and space complexity of arr.shift()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: Removing from the front of the array requires all other values to shift to the left by 1. This happens in place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the time and space complexity of arr.unshift()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: Adding to the front of the array requires all other values to shift to the right by 1 to make space for the first value. This happens in place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the time and space complexity of arr.splice()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: Splice will add or remove elements to the middle of the array in-place. This requires shifting all elements to the right of the new location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the time and space complexity of arr.slice()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(n)
Justification: This will create a new copy of the array with some elements sliced out. The remaining n values will need to be copied to the new location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the time and space complexity of arr.indexOf()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: This will search visit each item in the array until it finds the value. No extra space is needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the time and space complexity of arr.map()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(n)
Justification: Map creates a new
array (O(n) space) with some function mapped to each element. This is assuming the time complexity of the map callback has a time complexity of O(1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the time and space complexity of arr.filter()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(n)
Justification: Filter creates a new array (O(n) space) with a filter applied to each element. This is assuming the time complexity of the filter callback has a time complexity of O(1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the time and space complexity of arr.reduce()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: Reduce applies a reducer function to each element in the array. This is assuming the time complexity of the accumulator has a time complexity of O(1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the time and space complexity of arr.reverse()? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(1)
Justification: Reverse will move
all n elements in-place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the time and space complexity of […arr]? Explain your reasoning.

A

Time complexity: O(n)
Space complexity: O(n)
Justification: The spread operator will copy all n elements in the original array to a new array of length n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly