Space Complexity Flashcards

1
Q

What is space complexity?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What takes space?

A
variables
data structures
function call
allocations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Rule Book

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Interviews: What is the tradeoff between saving time and saving space?

A

you have to first decide which one you’re optimizing for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Twitter problem extended: boss asks you to compare the dates of tweets

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly