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

Time - Alicia #30

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

Time - Alicia #30

wants to merge 5 commits into from

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