Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is O(1) space complexity in term of Javascript code with example

For an example I have below mentioned input and output for function reverseWords()

It's a simple example but this will help me understand. How I will write a function which is In O(1) space for below request ?

// var input = ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r'];
// Output(same array): ['o', 'l', 'l', 'e', 'H', ' ', 'r', 'o', 'w']


// In O(1) space complexity 
function reverseWords(input) {

}

reverseWords(input);

How I will write a function which is In O(1) space ? For example -

function reverseString(str) {
    return str.split('').reverse().join('');;
}

reverseString("hello world");

Is this In O(1) Space complexity ? I have referred this What is O(1) space complexity? but still having doubt in terms of understanding in practical way of javascript.

like image 302
Javascript Coder Avatar asked Aug 06 '19 10:08

Javascript Coder


People also ask

What is space complexity of O 1?

a space complexity of O(1) means that the space required by the algorithm to process data is constant; it does not grow with the size of the data on which the algorithm is operating.

What is space complexity in Javascript?

Space Complexity: The space complexity of an algorithm quantifies the amount of space taken by an algorithm to run as a function of the length of the input.

What is space complexity with example?

Space complexity includes both Auxiliary space and space used by input. For example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criterion than Space Complexity.

Why is space complexity of binary search O 1?

In an iterative implementation of Binary Search, the space complexity will be O(1). This is because we need two variable to keep track of the range of elements that are to be checked. No other data is needed. In a recursive implementation of Binary Search, the space complexity will be O(logN).

What is the meaning of O(1) space complexity?

Simply speaking O (1) space complexity means that you use only a constant amount of memory in addition to the passed in values to produce the result. For example, if you use only a single primitive value in a variable, that would be a constant amount (because it's the same amount, no matter how big your input is)

What is'space complexity'in algorithms?

What is 'Space Complexity’? Space complexity is an amount of memory used by the algorithm (including the input values of the algorithm), to execute it completely and produce the result. We know that to execute an algorithm it must be loaded in the main memory. The memory can be used in different forms:

What is the difference between space complexity and time complexity?

Space complexity is a parallel concept to time complexity. If we need to create an array of size n, this will require O (n) space. If we create a two-dimensional array of size n*n, this will require O (n 2) space.

What is the space complexity of an array?

Space complexity is a parallel concept to time complexity. If we need to create an array of size n, this will require O (n) space. If we create a two-dimensional array of size n*n, this will require O (n 2) space. In recursive calls stack space also counts.


Video Answer


3 Answers

Simply speaking O(1) space complexity means that you use only a constant amount of memory in addition to the passed in values to produce the result.

For example, if you use only a single primitive value in a variable, that would be a constant amount (because it's the same amount, no matter how big your input is)

If your algorithm started with allocating an array as big as the input, then it would not be O(1), but O(n) instead (because the bigger the input the bigger the memory/space requirement is).

So as long as you ensure that your algorithm never allocates anything where the space requirement depends on the size of the input, then your algorithm has O(1) space requirements.

The reason you don't see anything specific to JavaScript in this answer is that big-O notations are pretty much independent of the underlying technology used. Every language will have some way to allocate variable-length things (such as strings as arrays) and fixed-length things ("simple" values such as numbers and booleans).

like image 158
Joachim Sauer Avatar answered Nov 14 '22 21:11

Joachim Sauer


space complexity of a function says something about how much memory does the function use to produce its output

O(1) space complexity is like saying "The function will not need more than some constant volume of memory regardless of what is on the input"

How much memory the function uses can vary of course but it cannot exceed that maximum

your first example with word reversal can be done "in place" similarly to an array reversal - the elements in the array are only reorganized in the existing memory provided as an input

function reverseArrayInPlace(arr) {
  let el = 0;
  for (var i = 0; i <= Math.floor((arr.length - 1) / 2); i++) {
      el = arr[i];
      arr[i] = arr[arr.length - 1 - i];
      arr[arr.length - 1 - i] = el;
  }
  return arr;
}

the only "memory allocation" the function does is to create 'el' that is reused

the second example where you want to reverse a string cannot be done in javascript - string is immutable (see https://www.sitepoint.com/immutability-javascript/ )so you would have to create a new string of the same length so you would use as much memory as is on the input making the fuction space complexity O(n)

Just one minor thing - real complexity is dependent on the implementation of the compiler/interpreter, when you talk about complexity you usually talk about theoretical complexity as some languages can copy arrays when they are edited etc. Most of the time people do not talk about "complexity in a specific language"

like image 31
Derte Trdelnik Avatar answered Nov 14 '22 22:11

Derte Trdelnik


O(1) space complexity means that the amount of memory you need to use to do the operation will not grow relative to the size of the collection that you are operating on. In the context of reversing words, O(1) space complexity would mean that an algorithm would be able to reverse the letters with a constant amount of memory (not necessarily the amount of memory taken up by the array), let's say 1MB, regardless of the length of the word.

In more concrete terms, you could potentially do this by using one extra character as swap space, and swapping letters opposite each other in the array (e.g. in the string 'hello', you would swap 'o' and 'h', then 'e' and 'l'. Because you only need one extra character in memory to do this regardless of the length of string you want to reverse, this counts as O(1) space complexity.

like image 42
Rob Streeting Avatar answered Nov 14 '22 21:11

Rob Streeting