Space Complexity Flashcards
What is space complexity?
Space complexity of an algorithm refers to the total space consumed by the algorithm with respect to input size.
Space complexity includes both auxiliary space and space consumed by input
What takes space?
variables data structures function call allocations
Rule Book
rule 1: always worst case
rule 2: remove constants
rule 3: different inputs should have different variables. O(a + b)
rule 4: drop non-dominant terms
Interviews: What is the tradeoff between saving time and saving space?
you have to first decide which one you’re optimizing for
Knowledge test: If you’re working at Twitter and your boss asks you to build a feature that allows a user to click a button next to their name to retrieve their oldest tweet and newest tweet.
based on Big O notation, what can we assume about this problem?
how would you solve this?
1st assumption: we must find 1st tweet
2nd assumption: we must find nth tweet
we don’t know how the tweets are stored, but based on how they’re stored we may be able to just grab the first entry from the database and the last entry from the database.
if the ds is an array, the Big O would be constant time
Twitter problem extended: boss asks you to compare the dates of tweets
tweets = [{ tweet: ‘whatup’, date: 2012 } , { tweet: ‘a bucket’, date: 2014 }, { tweet: ‘buckets’, date: 2018 }]
compare each tweet with the other tweets and compare their dates
this will be nested loops, which means it will be a Big O of O(n^2)