Skip to content

Leaves-Yasmin #24

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

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

Leaves-Yasmin #24

wants to merge 4 commits into from

Conversation

YasminM11
Copy link

Hash Table Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
Why is a good Hash Function Important? To provide a time complexity of O(1) for insert, delete and search
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 - Hash values are uniformly distributed - Keys that are close have hash values that are far apart
Is there a perfect hash function? If so what is it? Yes, A hash function for which there are no collisions is called a "perfect hash function"
Describe a strategy to handle collisions in a hash table Separate Chaining: uses a linked list to store the collided keys
Describe a situation where a hash table wouldn't be as useful as a binary search tree Hash tables are not good for finding ordered data. Linked list and binary search tree can get us the min and max in O(n) and O(log n) time.
What is one thing that is more clear to you on hash tables now I learned the ways to handle collisions in a hash table. Need more practice

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.

The first works well. The second happens to pass the tests, but doesn't actually work in general. Take a look at my comments and let me know if you have questions.

- Keys that are close have hash values that are far apart
- Hashing appears to be random (hard to deduce the original object – this is why hashing is also used in cryptography)|

3. Is there a perfect hash function? If so what is it? |Yes, A hash function for which there are no collisions is called a "perfect hash function"|

Choose a reason for hiding this comment

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

Except for any general purpose hash, no hash function can be perfect.

Comment on lines +5 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.

👍 Nice

Comment on lines +41 to +43
k.times do |element|
output << hash.sort[element][0]
end

Choose a reason for hiding this comment

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

I believe hash.sort sorts elements by the key instead of the value, so this will return the elements sorted by the keys instead of the frequency of their occurrence in the original array.

It's happening to pass the tests, but it's not working in general.

Consider this:

    it "will work for [3, 3, 3, 3, 3, 1, 1, 2, 2, 2]" do
      list = [3, 3, 3, 3, 3, 1, 1, 2, 2]
      k = 2

      answer = top_k_frequent_elements(list, k)

      expect(answer.sort).must_equal [2, 3]
    end

Also even if this did work, you're sorting the hash each iteration of the loop... which is excessive.

Comment on lines +25 to 27
# Time Complexity: o(n)
# 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.

This doesn't actually work (see below) and is O(k * n log n) in time complexity as you're sorting the hash k times.

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