Skip to content

Conversation

@aecombs
Copy link

@aecombs aecombs commented Sep 26, 2020

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? It can be good for storing data that you want organized into groups — like with names being sorted into buckets by the first letter, etc.
How can you judge if a hash function is good or not? If it's well balanced and has a good ratio of keys/buckets to values
Is there a perfect hash function? If so what is it? It's probably one that scales well...there might be a perfect one, contextually.
Describe a strategy to handle collisions in a hash table One way is to use an algorithm to determine where to put the data. It doesn't cluster like linear probing. Also, I just like it conceptually!
Describe a situation where a hash table wouldn't be as useful as a binary search tree If the data is sorted, it would make sense to use the btree.
What is one thing that is more clear to you on hash tables now The types of ways in which you can deal with collisions

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.

Not bad Alicia, you hit most of the learning goals and got most of the top_k_frequent_elements. Take a look at my comments and see if that would help you get to a solution, or if it gives you follow up questions to ask me.

Also take a look at my comments for the anagrams problem.

Comment on lines +11 to +14
bucket = anagram_hash.keys.find do |b|
unique_chars = b.chars.sort.join
unique_chars == str.chars.sort.join
end

Choose a reason for hiding this comment

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

Since you're using a hash you don't need to do a find here.

Instead consider:

letters = str.chars.sort
if anagram_hash[letters]
  anagram_hash[letters] << str
else
  anagrams_hash[letters] = [str]
end

end

#this doesn't pass tests — instead of grabbing the FIRST most frequent, it grabs the most frequent, period. I couldn't figure out how to do the other way
return elements_hash.keys.max_by(k) { |key| elements_hash[key] }

Choose a reason for hiding this comment

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

What you can do is find the maximum value in the hash, save the key in an answer array. Then delete that entry from the hash, repeat until you have an answer array that is k length. That would be O(nk) in time complexity.

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