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 - Diana #19

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

Space - Diana #19

wants to merge 2 commits into from

Conversation

dnguye2
Copy link

@dnguye2 dnguye2 commented Sep 15, 2020

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? It is a good data structure for inserting, deleting, and searching.
How can you judge if a hash function is good or not? - Fast computation of the hash value
- Hash values are uniformly distributed
- Keys that are close have hash values that are far apart
- Hashing appears to be random
Is there a perfect hash function? If so what is it? Yes, a direct addressing approach in which each key only has one value.
Describe a strategy to handle collisions in a hash table One strategy is separate chaining in which a linked list is used to store the keys that are colliding. The downside is that this requires additional memory.
Describe a situation where a hash table wouldn't be as useful as a binary search tree A dataset that has to be ordered and has many duplicate keys.
What is one thing that is more clear to you on hash tables now Dealing with collision events

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 Diana, you hit the learning goals here. Well done. You do have some work to do on space/time complexity. See my comments on these and let me know what questions you have.

Comment on lines +4 to 7
# Time Complexity: O(n)
# Space Complexity: O(1)

def grouped_anagrams(strings)

Choose a reason for hiding this comment

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

👍 , however you have O(n) space complexity.

array_num_hash = num_hash.to_a

# sort by occurences
sorted_array_num_hash = array_num_hash.sort_by {|num| -num[1]}

Choose a reason for hiding this comment

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

just noting this is an O(n log n) operation.

Comment on lines +26 to 28
# Time Complexity: O(n^2) in worst case because of the sorting to be done
# Space Complexity: O(1)
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.

You have an O(n log n) time complexity algorithm which is pretty good and O(n) space complexity because you're making a hash and an array both proportional to the size of the original list.

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