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 - Haben #23

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

Time - Haben #23

wants to merge 9 commits into from

Conversation

HabenFoto
Copy link

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? A Hash Function is important b/c it helps map a big number or string to a small integer that can be used as the index in the hash table.
How can you judge if a hash function is good or not? A good hash function has the following attributes: fast computation of the hash value, the hash value is fully determined by the data being hashed, the hash function "uniformly" distributes the data across the entire set of possible hash values and the hash function generates very different hash values for similar strings.
Is there a perfect hash function? If so what is it? Direct addressing is considered as the perfect hashing function as it has the capability of mapping records to their corresponding keys using arrays. In direct address tables, records are placed using their key values directly as indexes. They facilitate fast searching, insertion, and deletion operations.
Describe a strategy to handle collisions in a hash table One strategy to handle collisions is Separate Chaining, a scheme in which each position in the hash table has a list to handle collisions. Each position may be just a link to the list (direct chaining) or maybe an item and a link, essentially, the head of a list.
Describe a situation where a hash table wouldn't be as useful as a binary search tree Hash table depends on unique keys. If a dataset cannot provide unique key values, a hash table may not be the best solution. Instead, a binary search tree or another data structure may be a better selection.
What is one thing that is more clear to you on hash tables now The overall usage of Hash Functions and how easy or fast they make the process of search, insert or delete. And also the different strategies of how to resolve collision.

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 Haben, they both work. Your top_k_elements works, but uses an O^2 solution. I encourage you to think of an alternative, sorting is one way, there is also another even better.

Comment on lines +6 to 8
# Time Complexity: O(n)
# 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.

👍

Comment on lines +42 to 45
# Time Complexity: O(n^2)
# 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.

👍 Could you use a sort to improve the time complexity to O(n log n) or better?

# each element in another, sorted based on descending # freqency
elements = []
elements_frequency = []
hash1.each do | key, frequency |

Choose a reason for hiding this comment

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

Could you sort the hash key-value pairs here or select the max k elements instead?

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