an HCL GUVI product

Hashing - DSA Interview Questions - DSA Interview Questions

The Hashing technique efficiently stores and retrieves data using key–value pairs, allowing most operations such as search, insert, and delete to be performed in O(1) average time. It is widely used to solve problems involving frequency counting, duplicate detection, fast lookups, and data mapping.

Mastering hashing helps you tackle a wide range of DSA interview problems involving arrays, strings, and sets.

Learning Objectives:

Understand how hash tables (hash maps and hash sets) optimize time complexity for lookup and storage operations. Learn to apply hashing for frequency counting, detecting duplicates, and solving array and string problems efficiently. Identify when hashing is more suitable than sorting or brute-force approaches.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Problem 1 - Two Sum Problem

Required Input:

[2, 7, 11, 15] 
9

Expected Output:

[0, 1]

Code In Python

def two_sum(nums, target): # Write your logic here pass # Prefilled input nums = [2, 7, 11, 15] target = 9 print(two_sum(nums, target))

Run Code?

Click Run Button to view compiled output

2. Problem 2 - Check if an Array Contains Duplicates

Required Input:

[1, 2, 3, 1]

Expected Output:

True

Code In Python

def contains_duplicate(nums): # Write your logic here pass # Prefilled input nums = [1, 2, 3, 1] print(contains_duplicate(nums))

Run Code?

Click Run Button to view compiled output

3. Problem 3 - First Non-Repeating Character in a String

Required Input:

coding

Expected Output:

c

Code In Python

def first_unique_char(s): # Write your logic here pass # Prefilled input s = "coding" print(first_unique_char(s))

Run Code?

Click Run Button to view compiled output

4. Problem 4 - Intersection of Two Arrays

Required Input:

[1, 2, 2, 1] 
[2, 2]

Expected Output:

[2]

Code In Python

def intersection(nums1, nums2): # Write your logic here pass # Prefilled input nums1 = [1, 2, 2, 1] nums2 = [2, 2] print(intersection(nums1, nums2))

Run Code?

Click Run Button to view compiled output

5. Problem 5 - Find the Majority Element

Required Input:

[3, 3, 4, 2, 3, 3, 3, 1]

Expected Output:

3

Code In Python

def majority_element(nums): # Write your logic here pass # Prefilled input nums = [3, 3, 4, 2, 3, 3, 3, 1] print(majority_element(nums))

Run Code?

Click Run Button to view compiled output

6. Problem 6 - Group Anagrams

Required Input:

["eat", "tea", "tan", "ate", "nat", "bat"]

Expected Output:

[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Code In Python

def group_anagrams(words): # Write your logic here pass # Prefilled input words = ["eat", "tea", "tan", "ate", "nat", "bat"] print(group_anagrams(words))

Run Code?

Click Run Button to view compiled output

7. Problem 7 - Check if Strings Are Isomorphic

Required Input:

"egg" 
"add"

Expected Output:

True

Code In Python

def is_isomorphic(s1, s2): # Write your logic here pass # Prefilled input s1 = "egg" s2 = "add" print(is_isomorphic(s1, s2))

Run Code?

Click Run Button to view compiled output

8. Problem 8 - Find the Missing Number

Required Input:

[1, 2, 4, 5, 6]

Expected Output:

3

Code In Python

def find_missing_number(nums): # Write your logic here pass # Prefilled input nums = [1, 2, 4, 5, 6] print(find_missing_number(nums))

Run Code?

Click Run Button to view compiled output

9. Problem 9 - Longest Consecutive Sequence

Required Input:

[100, 4, 200, 1, 3, 2]

Expected Output:

4

Code In Python

def longest_consecutive(nums): # Write your logic here pass # Prefilled input nums = [100, 4, 200, 1, 3, 2] print(longest_consecutive(nums))

Run Code?

Click Run Button to view compiled output

10. Problem 10 - Single Number

Required Input:

[4, 1, 2, 1, 2]

Expected Output:

4

Code In Python

def single_number(nums): # Write your logic here pass # Prefilled input nums = [4, 1, 2, 1, 2] print(single_number(nums))

Run Code?

Click Run Button to view compiled output

1 of 3