10 DSA Hashing Question You Should Practice
10 DSA Hashing Question You Should Practice
HASHING
Question 1:
Two Sum III
Design and implement a TwoSum class. It should support
the following operations: add and find.
add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is
equal to the value.
Example 1:
add(1); add(3); add(5);
find(4) // return true
find(7) // return false
©www.csforall.in
Question 2:
Subsequence of Length K
ou are given an integer array nums and an integer k. You
want to find a subsequence of nums of length k that has the
largest sum.
Return any such subsequence as an integer array of length k.
A subsequence is an array that can be derived from another
array by deleting some or no elements without changing the
order of the remaining elements.
Example 1:
Input: nums = [2,1,3,3], k = 2
Output: [3,3]
Explanation:
The subsequence has the largest sum of 3 + 3 = 6.
Example 2:
Input: nums = [-1,-2,3,4], k = 3
Output: [-1,3,4]
Explanation:
The subsequence has the largest sum of -1 + 3 + 4 = 6.
©www.csforall.in
Question 3:
Longest Awesome Substring
You are given a string s. An awesome substring is a non-
empty substring of s such that we can make any number of
swaps in order to make it a palindrome.
Return the length of the maximum length awesome
substring of s.
Example 1:
Input: s = "3242415"
Output: 5
Explanation: "24241" is the longest awesome substring, we
can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "213123"
Output: 6
Explanation: "213123" is the longest awesome substring, we
can form the palindrome "231132" with some swaps.
©www.csforall.in
Question 4:
Avoid Flood in The City
Your country has an infinite number of lakes. Initially, all the
lakes are empty, but when it rains over the nth lake, the nth
lake becomes full of water. If it rains over a lake that is full of
water, there will be a flood. Your goal is to avoid floods in any
lake.
Given an integer array rains where:
rains[i] > 0 means there will be rains over the rains[i] lake.
rains[i] == 0 means there are no rains this day and you
can choose one lake this day and dry it.
Return an array ans where:
ans.length == rains.length
ans[i] == -1 if rains[i] > 0.
ans[i] is the lake you choose to dry in the ith day if rains[i]
== 0.
©www.csforall.in
Example 1:
Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There's no day to dry any lake and there is no flood in any
lake.
Example 2:
Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We
have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It's easy to prove that no
matter which lake you choose to dry in the 3rd day, the other
one will flood.
©www.csforall.in
Question 5:
Count Good Meals
A good meal is a meal that contains exactly two different
food items with a sum of deliciousness equal to a power of
two.
You can pick any two different foods to make a good meal.
Given an array of integers deliciousness where
deliciousness[i] is the deliciousness of the ithitem of food,
return the number of different good meals you can make
from this list modulo 10^9 + 7.
Note that items with different indices are considered
different even if they have the same deliciousness value.
Example 1:
Input: deliciousness = [1,3,5,7,9]
Output: 4
Explanation: The good meals are (1,3), (1,7), (3,5) and, (7,9).
Their respective sums are 4, 8, 8, and 16, all of which are
powers of 2.
©www.csforall.in
Question 6:
Ugly Number II
An ugly number is a positive integer whose prime factors are
limited to 2, 3, and 5.
Given an integer n, return the nth ugly number.
Example 1:
Input: n = 10
Output: 12
Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the
first 10 ugly numbers.
Example 2:
Input: n = 1
Output: 1
Explanation: 1 has no prime factors, therefore all of its prime
factors are limited to 2, 3, and 5.
©www.csforall.in
Question 7:
Word Ladder II
A transformation sequence from word beginWord to word
endWord using a dictionary wordList is a sequence of words
beginWord -> s1 -> s2 -> ... -> sk such that:
Every adjacent pair of words differs by a single letter.
Every si for 1 <= i <= k is in wordList. Note that beginWord
does not need to be in wordList.
sk == endWord
Given two words, beginWord and endWord, and a dictionary
wordList, return all the shortest transformation sequences from
beginWord to endWord, or an empty list if no such sequence
exists. Each sequence should be returned as a list of the words
[beginWord, s1, s2, ..., sk].
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList =
["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Explanation: There are 2 shortest transformation sequences:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"
©www.csforall.in
Question 8:
Word Break II
Given a string s and a dictionary of strings wordDict, add
spaces in s to construct a sentence where each word is a
valid dictionary word. Return all such possible sentences in
any order.
Note that the same word in the dictionary may be reused
multiple times in the segmentation.
Example 1:
Input: s = "catsanddog", wordDict =
["cat","cats","and","sand","dog"]
Output: ["cats and dog","cat sand dog"]
Example 2:
Input: s = "pineapplepenapple", wordDict =
["apple","pen","applepen","pine","pineapple"]
Output: ["pine apple pen apple","pineapple pen apple","pine
applepen apple"]
Explanation: Note that you are allowed to reuse a dictionary
word.
©www.csforall.in
Question 9:
Jump Game IV
Given an array of integers arr, you are initially positioned at
the first index of the array.
In one step you can jump from index i to index:
i + 1 where: i + 1 < arr.length.
i - 1 where: i - 1 >= 0.
j where: arr[i] == arr[j] and i != j.
Return the minimum number of steps to reach the last
index of the array.
Notice that you can not jump outside of the array at any
time.
Example 1:
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
Output: 3
Explanation: You need three jumps from index 0 --> 4 --> 3 --
> 9. Note that index 9 is the last index of the array.
©www.csforall.in
Question 10:
LFU Cache
Design and implement a data structure for a Least
Frequently Used (LFU) cache.
Implement the LFUCache class:
LFUCache(int capacity) Initializes the object with the
capacity of the data structure.
int get(int key) Gets the value of the key if the key exists in
the cache. Otherwise, returns -1.
void put(int key, int value) Update the value of the key if
present, or inserts the key if not already present. When
the cache reaches its capacity, it should invalidate and
remove the least frequently used key before inserting a
new item. For this problem, when there is a tie(i.e., two or
more keys with the same frequency), the least recently
used key would be invalidated.
©www.csforall.in
To determine the least frequently used key, a use counter is
maintained for each key in the cache. The key with the
smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is
set to 1 (due to the put operation). The use counter for a key
in the cache is incremented either a get or put operation is
called on it.
The functions get and put must each run in O(1) average
time complexity.
Example 1:
Input:
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get",
"get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
Output:
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
©www.csforall.in
Explanation:
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost
element is most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest,
invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU,
invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[4,3], cnt(4)=2, cnt(3)=3
©www.csforall.in