I was asked this in an interview: "Tell me everything you know about hashmaps."
I proceeded to do just that: it's a data structure with key-value pairs; a hash function is used to locate the element; how hash collisions can be resolved, etc.
After I was done, they asked: "OK, now explain everything you just said to a 5-year-old. You can't use technical terms, especially hashing and mapping."
I have to say it took me by surprise and I didn't give a good answer. How would you answer?
Explain the internal working of a HashMap.A hashmap uses a hashtable but what many people don't know is that it is internally created using two data structures namely an array and a linked list. Whenever you declare a hashmap what is going to happen internally is that it will create an array of buckets.
A hashmap is basically like a magic toy chest, where you can put something in and tell it the name (key) like this: "Toy chest: here is a 'ball'". Later, you can ask for things (values) by name: "Toy chest, give me the thing I told you was called 'ball'".
When storing a key that's already present in the HashMap, the HashMap size stays the same and doesn't throw error or exception. This is why all keys return Set instead of Collection when you call the keySet() method on HashMap since Set doesn't allow for duplicates."
get(key) which looks up the value for a key and returns it. If the key is not present in the map, get() returns null.
Rules. Kids know rules. Kids know that certain items belong in certain places. A HashMap is like a set of rules that say, given an item (your shoes, your favorite book, or your clothes) that there is a specific place that they should go (the shoe rack, the bookshelf, or the closet).
So if you want to know where to look for your shoes, you know to look in the shoe rack.
But wait: what happens if the shoe rack is already full? There are a few options.
1) For each item, there's a list of places you can try. Try putting them next to the door. But wait, there's something there already: where else can we put them? Try the closet. If we need to find our shoes, we follow the same list until we find them. (probing sequences)
2) Buy a bigger house, with a bigger shoe rack. (dynamic resizing)
3) Stack the shoes on top of the rack, ignoring the fact that it makes it a real pain to find the right pair, because we have to go through all of the shoes in the pile to find them. (chaining).
Lets take the big word book, or dictionary, and try to find the word zebra. We can easily guess that zebras will be near the end of the book, just like the letter "Z" is at the end of the alphabet. Now lets say that we can always find where the zebra is inside of the big word book. This is the way that we can quickly find zebras, or elephants, or any other type of thing we can think of in the big word book. Sometimes two words will be on the same page like apple and ant. We are sure which page we want to look at, but we aren't sure how close apple and ant are to each other until we get to the page. Sometimes apple and ant can be on the same page and sometimes they might not be, some big word books have bigger words.
That's how I would have done it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With