Tech IndustryMay 20, 2021
WalmartLCingNow

Space Complexity vs Time Complexity

I have been doing a lot of lc practice however I always almost struggle with space complexity when evaluating it. I slowly but surely realised not only your solution should be optimal in terms of time it should also be optimal in terms of space. So can the community please suggest me a good resource for learning this forte because most if not all courses are focused on time complexity.#tech #interviews #faang

Microsoft @Delphino May 20, 2021

following

Amazon Pipmastah May 20, 2021

The reason most university classes focus on time complexity and not so much on space complexity is because it's not that complex (see what I did there). You can have algorithms that have time complexities like O(A*B*log log C) and calculating that wouldn't be that straightforward. With space complexity, your space, given a particular input size, will usually be just constant, multiple or exponential times of the input size, and this is something you learn in middle school.

New
_usrname__ May 20, 2021

space complexity can also be log n

Adobe RVKL53 May 21, 2021

Yeah this guy is an idiot

Google d-ll-b May 20, 2021

Your solution should definitely be time optimal first. Then without sacrificing that, if you can find ways to use less memory then that’s a plus. I don’t think there’s a one size fits all answer to this, it really depends on the problem

Intuit BYun35 May 20, 2021

Well, O(1) implies constant space or just utilizing the input you've been provided. O(n) means an additional amount of space which is equal to the input size - like a copy of the input array(1D). If you're building a graph using a matrix it's O(n^2) but if you use adjacency lists it's O(n + m)(n,m are # of vertices and edges). (Of course, this is a very quick answer typed up on a phone. But, solve more questions with data structures as the focus and you'll understand how to compute space complexity )

Amazon __BRB May 21, 2021

Hey, don't we include input size in space complexity? Like if we are traversing an array of size n then won't space complexity be O(n) but auxiliary space used would be O(1).

Amazon fatcat420 May 21, 2021

AFAIK when people talk about space complexity it is assumed you are talking about auxiliary space

Yardi sticktwots May 20, 2021

Time complexity issues are more annoying to end user

LinkedIn hei5enberg May 20, 2021

Just like time complexity try to understand it in terms of operations. Searching in an array, do you need to store the values in an additional array, if yes how much space (in order of the input) does it take, can you do it without creating an additional array; what are the implicit operations you might be doing that adds to the space complexity (e.g recursion and adding to call stack).

LinkedIn hei5enberg May 20, 2021

Both might give valid solutions, more space optimal might be not creating an additional or using an iterative approach vs recursive. I don't think there is any central resource but study as many also as you can and understand each operation in terms of space and time. Sometimes one can be optimized over the other.

Walmart LCingNow OP May 21, 2021

Appreciate the detailed breakdown and explanation.

Facebook 👨‍⚕️ patt May 20, 2021

Explain that gravity warps space-time

Facebook PlusULTRA! May 21, 2021

Recent research actually theorizes that time is the cause of gravity. Fascinating stuff.

Facebook 👨‍⚕️ patt May 21, 2021

O(weird)

Oracle bliss-ful May 21, 2021

I haven’t come across any resource which explains space complexity in isolation, but there are many well written publications suggest - an algorithm must trade increased space usage with decreased execution time as memory now a days is very cheap. Also one has to have deep understanding of various DS and which one to choose over other while implementing … For example ‘Linked List’ can continue to expand without having to specify their size ahead of time unlike of Array - where one has to preload with initial Array size irrespective it is empty or has elements, and resizing Array is inefficient and will consume 2 X previous size every time you resize it … meaning more wastage of space and more time to resize !

Salesforce DogeeStyle May 21, 2021

Time is money. Space is not

Amazon a q p \n w May 21, 2021

Go try GPU programming where you have maximum ~36GB of global memory typically and people always want to run scientific simulations which are memory hogs you won’t be saying the same thing