Skip to content

Conversation

@mvlofthus
Copy link

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? it is efficient for quickly looking up data with unique keys
How can you judge if a hash function is good or not? if it has many collisions, it is likely not a good hash function
Is there a perfect hash function? If so what is it? a perfect hash function would have no collisions
Describe a strategy to handle collisions in a hash table chaining creates an array for a collision to store all data associated with that key, you can then look up the information by the key and then searching through the array. it is efficient if there are few collisions but approaches O(n) if there are many.
Describe a situation where a hash table wouldn't be as useful as a binary search tree if data cannot provide unique keys and needs to be maintained in a sorted order
What is one thing that is more clear to you on hash tables now when they are helpful to use and when they are not

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.

Nice work Mackenzie, you hit the learning goals here. Well done.

Comment on lines +4 to 7
# Time Complexity: O(nm(log m)) + O(n) (sort_by for each string + length of strings array)
# Space Complexity: O(n) (hash size increases with input array length, no other data structures made)

def grouped_anagrams(strings)

Choose a reason for hiding this comment

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

👍 Nice!

Comment on lines +22 to 24
# Time Complexity: O(n) + O(n logn) ( sort is nlogn, list.each is n, map is n)
# Space Complexity: O(n) (multiple same sized arrays)
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.

👍 So the final time complexity is O(n log n) because that's the bigger term.

Comment on lines +37 to +38
sorted = counted.sort {|a,b| b[1]<=>a[1]}
keys = sorted.map { |n| n[0] }

Choose a reason for hiding this comment

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

Very compact!

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