FAQ: Hash Maps: Conceptual - Hash Map Methodology

This community-built FAQ covers the “Hash Map Methodology” exercise from the lesson “Hash Maps: Conceptual”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Hash Map Methodology

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

If the goal of a hashing function is to produce an array index as an output, it should mean that the output of a hashing function should be unique(since two different keys cannot be stored in the same array index). However, why is then “adding the number of vowels in a word” a hashing function?

I mean, there can be two different words, like “coat” and “truce,” which has the same number of vowels. However, the hashing function output for both the words would be 2 in this case.

It would be great if someone could kindly answer this query of mine.

1 Like

How are Hash Maps and Dictionaries different? Aren’t dictionaries enough for key value pair data entry and access? If so, why do we need Hash Maps? I’m getting really confused about this, would appreciate any clarification.

3 Likes

You may have to do some digging to be sure but as far as I am aware Python’s dictionaries are currently implemented using one form of hash map (though the terminology can be a bit hazy). You could use your hash map to create your own dictionary class if you wished but there are other ways to get a working dictionary of this form. Practically in Python you’d be using the built-in for the most part but knowing how to implement your own is worthwhile. You may find you have a need for a specialised implementation one day or have the need to create one in a different language (though you’d likely use one that is already available).

You’d be learning about these structures in this course but for a higher level language like Python you will find thay many of them already in use, such as doubly linked lists, heaps and hash maps etc. which are either built into the language or are available from standard libraries.

7 Likes

Thank you ! I did dig around and found out that Hash Maps were used to implement Dictionary in Python, so a dictionary is essentially a Hash Map. It is clear to me now!
And learning about Hash Maps was a blast! Really glad I started this course :slight_smile:

1 Like

I have the same doubt too! Can someone please clarify this doubt?

Hashing functions aren’t perfect, a fixed sized hash will always have the possibility of collisions, what you want though is a very low probability of collisions. A good hashing function should minimise the number of collisions, the number of vowels in a word would be an example of a poor hashing function.

For a hash map you would probably need some additional coding to deal with collisions for this reason (this is even more important in OOP where object equivalence may be more common than expected). I’m sure it’ll pop up in due course.

1 Like

I believe the hashing function output wouldn’t be 2 for both. As you said, the hashing function means a unique hash (number) assigned to the vowel. So for example, if a = 0, e = 1, i = 2, o = 3, u = 4., then summing the hashes of “coat” would equal 3 while summing the hash for “truce” would equal 5.

Is it necessary that hash maps have strings as keys?

I know that this is how hash maps are typically implemented in languages like Python or JS, but is there anything preventing one from using, say, numbers as keys? Like so:
1: Bob S.
2: Claude C.
3: Sarah F.

Then the numeric key could correspond to something like an ID and the hash table maps ID to full name.

You can use other types than just strings provided your hashing function can handle them. Python’s for example accept several different types as keys in the same dictionary provided the keys are what it calls, hashable: https://docs.python.org/3/glossary.html#term-hashable

It’s just a bit more difficult to write a good hashing function that accepts multiple types, it’s easier to use a single type and I’d assume there’s less chance of collision with a single type.

what about a word like boat and coat? they would have the same hashing function, right?