Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview Question: What is a hashmap? [closed]

Tags:

hashmap

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?

like image 949
999999 Avatar asked Oct 07 '10 21:10

999999


People also ask

What is HashMap explain in interview?

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.

How would you explain HashMap to a non technical person?

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'".

What happens if you try to store a key that is already present in HashMap?

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."

What will happen if we get key from HashMap which is not present in map?

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.


2 Answers

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).

like image 92
James Avatar answered Sep 18 '22 10:09

James


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.

like image 36
Woot4Moo Avatar answered Sep 19 '22 10:09

Woot4Moo