Hash tables are extremely useful data structures as lookups take expected O(1) time on average, i.e. the amount of work that a hash table does to perform a lookup is at most some constant. Several data structures and algorithm problems can be very efficiently solved using hashing which otherwise has high time complexity.
In this post, we will list out few problems that can be solved elegantly using hashing, with a significant economy of time and space.
std::set
,std::map
,std::unordered_set
,std::unordered_map
in C++HashMap
,HashSet
,TreeMap
in JavaDictionary
,Set
in Python
We recommend getting yourself familiar with the above data structures before proceeding to the following hashing problems.
- Find a pair with the given sum in an arrayEasy
- Check if a subarray with 0 sum exists or notMedium
- Print all subarrays with 0 sumMedium
- Find maximum length subarray having a given sumMedium
- Find maximum length subarray having an equal number of 0’s and 1’sMedium
- Find the largest subarray formed by consecutive integersMedium
- Find majority element (Boyer–Moore Majority Vote Algorithm)Easy
- Find a subarray having the given sum in an integer arrayMedium
- Find a triplet with the given sum in an arrayMedium
- Find the longest continuous sequence length with the same sum in given binary arraysHard
- Find pairs with difference
k
in an arrayEasy - 4–Sum Problem | Quadruplets with a given sumMedium
- Group elements of an array based on their first occurrenceMedium
- Find all symmetric pairs in an array of pairsMedium
- Find the count of distinct elements in every subarray of size
k
Medium - Print all subarrays of an array having distinct elementsMedium
- Find the minimum index of a repeating element in an arrayEasy
- Find an index of the maximum occurring element with equal probabilityEasy
- Check if an array is formed by consecutive integersMedium
- Find two non-overlapping pairs having the same sum in an arrayMedium
- Count distinct absolute values in a sorted arrayMedium
- Find subarrays with a given sum in an arrayMedium
- Efficiently calculate the frequency of all elements present in a limited range arrayMedium
- Shuffle an array according to the given order of elementsMedium
- Find duplicates within a range
k
in an arrayEasy - Find the longest subsequence formed by consecutive integersMedium
- Determine whether an array can be divided into pairs with a sum divisible by
k
Medium - Sort elements by their frequency and indexMedium
- Sort an array based on order defined by another arrayMedium
- Efficiently sort an array with many duplicated valuesMedium
- Find surpasser count for each array elementHard
- Find all common elements present in each row of a matrixMedium
- Find common elements present in all rows of a matrixMedium
- Check if a repeated subsequence is present in a string or notHard
- Determine whether two strings are anagram or notEasy
- Isomorphic StringsMedium
- Find all possible combinations by replacing given digits with characters of the corresponding listHard
- >Find all words that follow the same order of characters as given patternMedium
- Group anagrams together from a list of wordsMedium
- Find minimum operations required to transform a string into another stringHard
- Find the longest substring of a string containing
k
distinct charactersHard - Find all palindromic permutations of a stringMedium
- Find all substrings of a string that are a permutation of another stringMedium
- Construct the longest palindrome by shuffling or deleting characters from a stringMedium
- Find the first non-repeating character in a string by doing only one traversal of itMedium
- Find all substrings containing exactly
k
distinct charactersMedium - Determine whether a string matches with a given patternHard
- Find the odd occurring element in an array in a single traversalEasy
- Find two odd occurring elements in an array without using any extra spaceMedium
- Find the duplicate element in a limited range arrayMedium
- Find two duplicate elements in a limited range array (using XOR)Medium
- Print bottom view of a binary treeMedium
- Print top view of a binary treeMedium
- Find ancestors of a given node in a binary treeMedium
- Find the diagonal sum of a binary treeMedium
- Iteratively print the leaf to root path for every leaf node in a binary treeMedium
- Build a binary tree from a parent arrayHard
- Construct a binary tree from inorder and preorder traversalHard
- Construct a binary tree from inorder and postorder traversalsHard
- Construct a binary tree from inorder and level order sequenceHard
- Construct a full binary tree from a preorder and postorder sequenceHard
- Find postorder traversal of a binary tree from its inorder and preorder sequenceMedium
- Find preorder traversal of a binary tree from its inorder and postorder sequenceHard
- Clone a binary tree with random pointersHard
- Construct a binary tree from an ancestor matrixHard
- Find a pair with the given sum in a BSTEasy
- Find the frequency of each element in a sorted array containing duplicatesEasy
- Find pairs with difference
k
in an array | Constant Space SolutionMedium - 3–Partition ProblemMedium
- Find the probability that a person is alive after taking
n
steps on an islandMedium - Find all employees who directly or indirectly reports to a managerHard
- Find correct order of alphabets in a given dictionary of ancient originHard
- Graph Coloring ProblemMedium
- Find itinerary from the given list of departure and arrival airportsEasy
- Find first
k
non-repeating characters in a string in a single traversalMedium - Replace each array element by its corresponding rankEasy
- Detect cycle in a linked list (Floyd’s Cycle Detection Algorithm)Easy
- Remove duplicates from a linked list in a single traversalEasy
- Clone a linked list with random pointerHard
- Link nodes present in each level of a binary tree in the form of a linked listHard
- Find the intersection point of two linked listsMedium
- Find the vertical sum of a binary treeHard
- Find numbers represented as the sum of two cubes for two different pairsMedium
- Determine if two integers are equal without using comparison and arithmetic operatorsEasy
- Efficiently print all nodes between two given levels in a binary treeEasy
- Level order traversal of a binary treeEasy
- Spiral order traversal of a binary treeMedium
- Reverse level order traversal of a binary treeEasy
- Print all nodes of a perfect binary tree in a specific orderHard
- Print left view of a binary treeEasy
- Print diagonal traversal of a binary treeMedium
- Convert a binary tree into a doubly-linked list in spiral orderHard
- Perform vertical traversal of a binary treeMedium
- Compute the maximum number of nodes at any level in a binary treeEasy
- Print right view of a binary treeMedium
- Memory Efficient Implementation of Trie in C++ – Insert, Search and DeleteBeginner
- Find duplicate rows in a binary matrixMedium
- Generate a list of possible words from a character matrixHard
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)