Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linnea - Branches #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Linnea - Branches #23

wants to merge 4 commits into from

Conversation

llinneaa
Copy link

@llinneaa llinneaa commented Mar 9, 2020

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? A hash table is essentially a dictionary, and implementing a good hash function enables faster look-up by minimizing the number of collisions and handling them appropriately.
How can you judge if a hash function is good or not? The hashing function must be consistent, should (mostly) map different keys to different values, should execute in constant time, and should appear to be random.
Is there a perfect hash function? If so what is it? No, because they can all cause collisions.
Describe a strategy to handle collisions in a hash table Chaining: makes each bucket of the hash table's internal array the head of a linked list.
--
Describe a situation where a hash table wouldn't be as useful as a binary search tree When elements need to be sorted.
What is one thing that is more clear to you on hash tables now How they are stored in memory.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall well done, see my minor comments. You hit all the learning goals here. Nice work.

Comment on lines +5 to 8
# Time Complexity: O(n*m)
# Space Complexity: O(n)

def grouped_anagrams(strings)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but your time complexity is O(n * m log m) where n is the number of strings and m is the length of the strings since you are sorting.

If the strings are guaranteed to be within a certain length, you could drop the m term and it would be O(n).

end

k.times do
max_key = hash.key(hash.values.max)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.key searches the values array for a matching value and returns it's key. So .key is O(n) (not to mention that .max is also O(n).

Comment on lines +26 to 28
# Time Complexity: O(n * m) where n is the length of the list and m is the size of k
# Space Complexity: O(n + m) where n is the hash and m is the elements results array
def top_k_frequent_elements(list, k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, although I would have put time complexity as O(nk) instead of O(nm).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants