Skip to content

Conversation

@anyatokar
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 is important for a well-functioning and efficient hash map.
How can you judge if a hash function is good or not? 1. It's consistent (h(key) always results in same number). 2. Maps different keys to different values (for the most part). 3. Executes in constant time (efficient). 4. Should appear to be random (but reversible in hash tables).
Is there a perfect hash function? If so what is it? No, there isn't. Looking for a function that is easy to compute, maps input correctly, and minimizes collisions.
Describe a strategy to handle collisions in a hash table One strategy is chaining, where each element of the hash table's internal array is a head of a linked list.
Describe a situation where a hash table wouldn't be as useful as a binary search tree Keeping track of something in order by key.
What is one thing that is more clear to you on hash tables now Learned what a hash function is.

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.

Nice work Anya, I had some feedback regarding space/time complexity. Hopefully my feedback here is clear. Let me know if it's not and I'm happy to try to clarify.

Comment on lines +4 to 8
# Time Complexity: O(n * m) n is the length of the array, m is the max length of string within the array.
# Could pull chars method to run first on each element of strings, for O(n) n being the longest string or array.
# 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.

👍 But the time complexity is O(n) if the strings are limited in size (so you can ignore the sort because it's limited in size), or O(n * m log m) where m is the length of the strings, if the string is not limited in size.

Comment on lines +21 to 23
# Time Complexity: O(n) n is list length
# Space Complexity: O(n^2 * k) is that right?
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.

👍 The time complexity is O(n log n) because you have an O(n) loop (the list.each loop) then a sort (O(n log n)), and followed by another loop which goes k times where k <= n.

So the time complexity is O(n + n log n + k) ==> O(n log n) because n log n is the dominate term. It's larger than n and k.

In the space complexity you're creating a hash O(n), a sorted hash O(n), and k_most_common_ele. So O(n + n + k) and so the final space complexity is O(n).

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