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

Space - Hala #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Space - Hala #18

wants to merge 1 commit into from

Conversation

halahaddad1
Copy link

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? A good hash function can keep the lookup time closest to a O(1)
How can you judge if a hash function is good or not? On how close to a O(1) the item lookup time is, basically the less the collisions the better, also how it deals with collisions.
Is there a perfect hash function? If so what is it? A perfect hash function is one with zero collisions.
Describe a strategy to handle collisions in a hash table linear probing: which looks into the hash table and tries to find another open slot to hold the item that caused the collision.
Describe a situation where a hash table wouldn't be as useful as a binary search tree A situation that depends on the order of items.
What is one thing that is more clear to you on hash tables now The concept of collisions is definitely more clean now.

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, you have all the solutions working. You can do things a bit more efficiently using hashes a bit more. This is something to practice a bit as you prepare for interviews.

Comment on lines +4 to 7
# Time Complexity: O(n2)
# 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 you can do better by sorting the words and using the sorted letters as keys for all the anagrams


numHash.keys.each do |num|
if returnList.length < k
returnList<< num

Choose a reason for hiding this comment

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

Suggested change
returnList<< num
returnList<< num

numHash = {}
returnList = []
list.each do |num|
if numHash.keys.include?(num)

Choose a reason for hiding this comment

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

Can be simplified

Suggested change
if numHash.keys.include?(num)
unless numHash[num].nil?

Comment on lines +32 to 34
# Time Complexity: O(n2)
# Space Complexity: O(n)
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.

Your solution has 3 loops which each run n times. It also sorts the array. So the method is O(n log n) 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