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
6
votes
3 answers

what type of maths is required to understand algorithm time and space complexities?

I've been applying for jobs and every time I hear questions about algorithm time/space complexity, I cringe and stumble. No matter how much I read, my brain seems to be programmed to not get any of it, and I think the reason is down to the fact I…
CaseyJones
  • 495
  • 1
  • 5
  • 17
5
votes
2 answers

Time complexity versus space complexity in Turing machines

I think defenitions of time complexity and space complexity for Turing machines are identical and I can't differentiate between them. Please help me. Thanks.
5
votes
5 answers

Find Kth largest number in single pass without storing the whole array

The algo I have in mind is keep an MaxHeap of size K insert each element drop out smaller value if heap is full In the end, Kth max is the smaller of MaxHeap That will give me O(NlogK). Is there a better algorithm? I can't do quick selection,…
Wei Shi
  • 4,945
  • 8
  • 49
  • 73
5
votes
1 answer

"Three sums" problem space complexity - Why is it O(n)?

Leetcode - Three sums https://leetcode.com/problems/3sum/ def threeNumberSum(array, targetSum): array = sorted(array) results = [] for idx, elem in enumerate(array): i = idx + 1 j = len(array) - 1 target =…
zcahfg2
  • 861
  • 1
  • 12
  • 27
5
votes
3 answers

Implementation of LinkedList in python __getitem__() method

I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): …
5
votes
2 answers

Space complexity of the piece of code below?

I came across this question while I was doing some interview prep. public class Main { public static void main(String[] args) { // n is some user input value int i = 0; while (i < n) { int[] a = new int[n]; …
amitection
  • 2,696
  • 8
  • 25
  • 46
5
votes
1 answer

Haskell; performance of where clause

I was analyzing the effect of where clauses on performance of Haskell programs. In Haskell, The craft of functional programming, Thomspson, chapter 20.4, I found the following example: exam1 :: Int -> [Int] exam1 n = [1 .. n] ++ [1 .. n] exam2 ::…
5
votes
2 answers

Space complexity for recursive calls

I am reading Cracking the Code Interview 6th Edition and have a question about something on page 45. There is an example algorithm like this: int f(int n){ if (n <= 1) return 1; return f(n - 1) + f(n - 1); } For the algorithm it…
Kou Chibin
  • 73
  • 8
5
votes
1 answer

Time Complexity for Spark/Distributed Algorithms

If we have below time complexity for some sequential algorithm, how can we express this time complexity for the same algorithm implemented in Spark (distributed version). Assuming that we have 1 master node and 3 worker nodes in the…
5
votes
2 answers

What is the space complexity for an iterative preorder traversal in a Binary tree?

I have been wondering what would be the space complexity for an iterative preorder traversal(using stack) for a Binary tree. I have referenced Elements of Programming Interviews and they stated that The space complexity is O(h), where h is the…
5
votes
1 answer

Space & Time Complexity of SHA-2

I am wondering what the space and time complexity of SHA-2 is. I tried looking around, didn't really get a straight forward answer. Can anyone help me out? Thanks very much!
vs9999
  • 61
  • 4
5
votes
1 answer

How is this space complexity calculated in this series sum?

Can someone explain the following space complexity calculation to me? Given a stream of numbers of size b bits, calculate the sum of these numbers. If we have seen T numbers so far, the sum is at most T2^b and hence needs at most O(b+log T)…
jcm
  • 5,499
  • 11
  • 49
  • 78
5
votes
1 answer

How to Compute Space Complexity for Binary SubTree Finding

This problem is from the book Cracking the Coding Interview. I have trouble understanding the space complexity of the solution. Problem: You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an…
jjwest
  • 113
  • 5
5
votes
1 answer

What are the space complexities of inits and tails?

TL; DR After reading the passage about persistence in Okasaki's Purely Functional Data Structures and going over his illustrative examples about singly linked lists (which is how Haskell's lists are implemented), I was left wondering about the space…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
5
votes
2 answers

What exactly does O(n) space complexity mean and how inefficient is it?

I have a high level understanding of what O(n) means in space. It means something along the lines of that for an algorithm with input n, the additional storage in memory allocated by this algorithm will increase proportionally to n. So if you had…