Questions tagged [space-complexity]

The space complexity of an algorithm quantifies the amount of memory taken by an algorithm to run as a function of the size of the input to the problem. The space complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

The space complexity of a program (for a given input) is the number of elementary objects that this program needs to store during its execution. This number is computed with respect to the size n of the input data.
Formally, for an algorithm T and an input x, DSPACE(T, x) denotes the number of cells used during the (deterministic) computation T(x). We will note DSPACE(T) = O(f (n)) if DSPACE(T, x) = O(f (n)) with n = |x | (length of x).

923 questions
3
votes
1 answer

Why is Merge sort space complexity O(n)?

At first look, it makes sense that merge sort has space complexity of O(n) because to sort the unsorted array I'm splitting and creating subarrays but the sum of sizes of all the subarray will be n. Question : The main concern that I have is that…
black sheep 369
  • 564
  • 8
  • 19
3
votes
4 answers

Is there a sorting algorithm with linear time complexity and O(1) auxiliary space complexity?

Is there a sorting algorithm with linear time complexity and O(1) auxiliary space complexity to sort a list of positive integers? I know that radix sort and counting sort have linear time complexity (O(kn) and O(n+k) respectively if we take k as…
Mathphile
  • 185
  • 1
  • 9
3
votes
1 answer

What is the space complexity of BST?

function validateBst(root, min=-Infinity, max=Infinity) { if (!root) return true; if (root.value <= min || root.value > max) return false; return validateBst(root.left, min, root.value) && validateBst(root.right,root.value, max) } Can…
3
votes
4 answers

Deciding whether a string is a palindrome

This is a python question. Answer should be with O(n) time complexity and use no additional memory. As input i get a string which should be classified as palindrome or not (palindrome is as word or a phrase that can be read the same from left to…
3
votes
1 answer

How can I solve this dynamic programing problem?

I was stuck in a problem studying dynamic programming. I have a string of numbers. You need to find the length of the longest substring of the substrings in this string that has the sum of the first half of the numbers and the second half of the…
3
votes
2 answers

Space Complexity of the Priority Queue in this Dijkstra Algorithm

Can anyone tell me the space complexity of the priority queue in this Dijkstra algo. Note that here one vertex can be added to queue more than one time. However, due to the visited set it is not processed more than one time. This is why I am…
cs guy
  • 926
  • 2
  • 13
  • 33
3
votes
3 answers

Is fixed array size O(n) or O(1) in space?

Is an array declared like this: int array[M], O(1) in space or O(n)? where M is some fixed value. To me O(n) makes sense because it is not just a single variable but an entire array. But then i think it could be O(1) since we have a fixed size and…
infinitloop
  • 2,863
  • 7
  • 38
  • 55
3
votes
1 answer

Output array numbers with their negation in O(n) time and O(1) space

Given an array of integers. If the number a and its negation -a both present in the array then print it. ex:if {10, 5, 0, 9, -10, 7, -5} is given then print 10, 5. I gave the interviewer O(N) time and O(N) Space complexity code based on HashMap but…
3
votes
1 answer

Time and space complexity of Ruby Anagram algorithm

I have written this algorithm here and I am trying to evaluate its time and space complexity in terms of Big-O notation. The algorithm determines if two given strings are anagrams. def anagram(str1, str2) str1.each_char do |char| …
Henry
  • 73
  • 5
3
votes
0 answers

What's the time and space complexity of the PIVOT operation in Spark SQL?

Recently, I'm working on a big key-name-value dataset. I want to group by on the name, pivot on the key, and select the first value for those to generate new columns. The operation is the following (in spark sql): val df:…
username_HI
  • 115
  • 1
  • 2
  • 5
3
votes
1 answer

How the space complexity of this algorithm is O(1)

I found an algorithm here to remove duplicate characters from string with O(1) space complexity (SC). Here we see that the algorithm converts string to character array which is not constant, it will change depending on input size. They claim that it…
Moshi
  • 1,385
  • 2
  • 17
  • 36
3
votes
1 answer

What is the complexity of strided slice in Tensorflow?

I am wondering what is the complexity of the strided slice function in Tensorflow. Obviously, it is not as computationally intensive as a Convolution 2D, but it's certainly not free neither. I'm not even sure talking of complexity for this operation…
Joseph Budin
  • 1,299
  • 1
  • 11
  • 28
3
votes
2 answers

Why numpy.var is O(N) space?

I have an array of ~13GB. I call numpy.var on it to compute the variance. However, it allocates another ~13GB to do this. Why does it need O(N) space? Or am I calling numpy.var in a wrong way? import numpy as np # data = ... print('Variance: ',…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
3
votes
3 answers

Space complexity of an array of pairs

So I'm wondering what the space complexity of an array of integer pairs is? std::pair arr[n]; I'm thinking that, because a pair is constant and the array is n, the space complexity is O(2) * O(n) = O(2n) = O(n). Or is the space complexity…
David
  • 103
  • 1
  • 1
  • 8
3
votes
1 answer

In git, at what rate does the size increase if every character change is commit?

I'm trying to understand how Git works. If I were to change (add or remove) character, save and commit that change until my code is written how will the size increase as the file becomes bigger? For example, what would be the size increase…
babygirl
  • 73
  • 6