DSA Day-19

Arya Goswami
placement_preparation
3 min readAug 31, 2020

Hey Everyone!!

Hope you’re doing well. I understanding how exhausting and boring staying in can be, but guys, please take care of your health. Some studies show that COVID-19 will be on peak soon.

Continuing our DSA preparation, today we’ll be learning about a very important topic : HASHMAP

Now, during my Amazon interview, the interviewer asked me a spiral of questions on concepts of hashmap… the questions were straightforward, yet, very confusing and required in-depth knowledge of the topic.

Hashing

Hashing is a method of storing and retrieving data from a database efficiently.

Example: Suppose we want to design a system for storing employee records keyed using phone numbers. And we want following queries to be performed efficiently:

  1. Insert a phone number and corresponding information.
  2. Search a phone number and fetch the information.
  3. Delete a phone number and related information.

Why use hashing when we can use Arrays, Linked list or any other Data Structure?

  • Hashing is an improvement over Direct Access Table. The idea is to use hash function that converts a given phone number or any other key to a smaller number and uses the small number as index in a table called hash table.
  • Time complexity for searching any key is O(1) on average while in worst case it is O(n)

PS: What is the time complexity for searching in arrays? Linked List?

Hash Function

A function that converts a given big phone number to a small practical integer value. The mapped integer value is used as an index in hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as index in hash table.
A good hash function should have following properties:

  1. Efficiently computable.
  2. Should uniformly distribute the keys (Each table position equally likely for each key)

Hash Table

An array that stores pointers to records corresponding to a given phone number. An entry in hash table is NIL if no existing phone number has hash function value equal to the index for the entry.

Collision Handling: Since a hash function gets us a small number for a big key, there is possibility that two keys result in same value. The situation where a newly inserted key maps to an already occupied slot in hash table is called collision and must be handled using some collision handling technique. Following are the ways to handle collisions:

  • Chaining:The idea is to make each cell of hash table point to a linked list of records that have same hash function value. Chaining is simple, but requires additional memory outside the table.
  • Open Addressing: In open addressing, all elements are stored in the hash table itself. Each table entry contains either a record or NIL. When searching for an element, we one by one examine table slots until the desired element is found or it is clear that the element is not in the table.

These are few important concepts of hashmap and trust me, I was asked about these concepts for around 20 minutes during my Amazon interview.

So, don’t take theoretical concepts lightly, because their knowledge can make a difference in your interview.

For java, refer to the following link for studying about the syntax and other important concepts of hashmap :

For practice, refer the following link:

HashMap is a very easy and important concept, so make sure you learn it thoroughly, because it’s very useful and very easy to understand with very simple implementation.

--

--

Arya Goswami
placement_preparation

Incoming SDE intern at Amazon || Ex- mentee at Amazon ACMS