Skip to content

Conversation

shubha-rajan
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 makes insertion and lookup of elements as effficient as possible.
How can you judge if a hash function is good or not? A good hash function minimizes collisions and efficiently handles collisions when they do occur
Is there a perfect hash function? If so what is it? Perfect hash functions, or hash functions with no collisions, are only possible if the set of key values is known in advance (reference: http://courses.cs.vt.edu/~cs3114/Fall09/wmcquain/Notes/T17.PerfectHashFunctions.pdf)
Describe a strategy to handle collisions in a hash table One strategy to handle collisions in hash tables is Separate Chaining, where values are stored in linked lists at every index. When there is a collision, the new value is appended to the linked list. Another method is open addressing, where there is both a hashing and incrementing function, and if a hash index is already full, the incrementing function is called until there is an open address to store the data. Some collision handling methods can be a combination of these two approaches.
Describe a situation where a hash table wouldn't be as useful as a binary search tree If you need to be able to iterate through keys in a specific order, or find a min or max key.
What is one thing that is more clear to you on hash tables now The different ways hash tables handle 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.

Thanks for getting this in. Nice work, you hit all the learning goals here. Well done!

Comment on lines +5 to +6
# Time Complexity: O(n * m log m) where m is the number of strings, and m is the length of a string
# Space Complexity: O(n * m) where n is the number of strings, and m is the length of a string (m space complexity from sort)

Choose a reason for hiding this comment

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

👍

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n * k), where n is the length of the list and k is the number of common elements to return
# Space Complexity: O(n + k) where n is the length of the list and k is the number of common elements to return

Choose a reason for hiding this comment

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

Since you're building a hash of unique elements I would say that dominates the time complexity.

So O(m) where m is the number of unique elements.

Comment on lines +92 to +101
counts = Hash.new(0)
(row_start...row_end).each do |row_index|
(col_start...col_end).each do |col_index|
char = table[row_index][col_index]
if char.to_i.to_s == char
counts[char] += 1
return false if counts[char] > 1
end
end
end

Choose a reason for hiding this comment

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

Nice, although you might be able to reduce the complexity of this loop a bit by creating a method.

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