August 2024
Intermediate to advanced
516 pages
11h 47m
English
These are the solutions to the exercises found in the section Exercises.
The space complexity is O(N2). This is because the function creates the array called collection, which will end up holding N2 strings.
This implementation takes up O(N) space, as we create a newArray containing N items.
The following implementation uses this algorithm: we swap the first item with the last item in place. Then we swap the second item with the second-to-last item in place. We then proceed to swap the third item with the third-to-last item in place, and so on. Since everything is done in place and we don’t create any new data, this has a space complexity of O(1).
| | function reverse(array) { |
| | let i = 0; |
| | |
| | while (i < Math.floor(array.length ... |
Read now
Unlock full access