Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a:

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your career...

Current Company *
Enter company name
Experience *

You're all set!

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Hashing

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

Hashing Implementation Details

C

C has nothing native for hashmap We need to write one ourselves.

C++ :

C++ has unordered_map which is implemented via hashing. We will discuss treemaps in C++ as well later, which in practice seem to behave faster in C++.

Declaration
    unordered_map<int, int> A; // declares an empty map. O(1)
Adding an key, value pair :
    A.insert({key, value}); // O(1) time on average
Find the value for key = K:
    if (A.find(K) == A.end()) return null; //  means that K does not exist in A. 
    else return A[K];    // O(1) average case. Rare worst case O(n). 
Size ( number of elements ) of the vector :
    A.size()  // O(1)
Erase from the map :
    if (A.find(K) != A.end()) A.erase(A.find(K));
     OR A.erase(K);
JAVA :

Now we come to one of the most popular data structures in Java, HashMap.

Declaration :
    HashMap<Integer, Integer> A = new HashMap<Integer, Integer>(); // declares an empty map.
Adding an key, value pair :
    A.put(key, value); // O(1) time on average
Find the value for key = K:
    A.get(K) // null if the key K is not present
    A.containsKey(K) tells if the key K is present. 
    // Both operations O(1) average time. O(n) rare worst case
Size ( number of elements ) of the vector :
    A.size()  // O(1)
Erase from the map :
    A.remove(K);
PYTHON

Python has dictionaries which serve the same purpose.

Declaration :
    A = {}
Adding an key, value pair :
    A[key] = value // O(1) average time. O(n) worst case
Find the value for key = K:
    A[K] // O(1) average, O(n) worst
Size ( number of elements ) of the vector :
    len(A) // O(1) 
Erase from the map :
        del A[K] // O(1) average, O(n) worst

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Hashing Problems

Hash search
Problem Score Companies Time Status
Colorful Number 150 44:55
Largest Continuous Sequence Zero Sum 200
68:44
Longest Subarray Length 200 53:40
First Repeating element 200 21:09
2 Sum 300 49:18
4 Sum 325 71:56
Valid Sudoku 325 50:21
Diffk II 375
29:05
Key formation
Problem Score Companies Time Status
Pairs With Given Xor 200 26:18
Anagrams 350 46:36
Equal 350
71:01
Copy List 450 55:56
Maths and hashing
Problem Score Companies Time Status
Check Palindrome! 200 17:47
Fraction 450 81:28
Points on the Straight Line 450 78:00
Incremental hash
Hashing two pointer
Free Mock Assessment
Help us know you better for the best experience
Current Employer *
Enter company name
College you graduated from *
Enter university name
Phone Number *
OTP will be sent to this number for verification
+1
+1
Change Number
Edit
Resend OTP
By Continuing I agree to be contacted by Scaler in the future.
Already have an account? Log in