HashMap
03 February 2026 18:39
Got it
I’ll give you ALL 10 TOP HASHMAP INTERVIEW QUESTIONS with:
• ✅ Problem idea (in simple words)
• HashMap logic used
• Core pattern to remember
• Clean beginner-friendly Java code
You can study this like a mini syllabus.
TOP 10 HASHMAP INTERVIEW QUESTIONS (WITH
LOGIC)
Problem
Find two numbers whose sum equals the target.
HashMap idea
Store:
number → index
Pattern
“Have I already seen (target − current)?”
Java Code
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < [Link]; i++) {
int need = target - nums[i];
if ([Link](need)) {
return new int[]{[Link](need), i};
}
[Link](nums[i], i);
}
return new int[]{};
}
Problem
Find the longest substring with no repeated characters.
HashMap idea
Store:
character → last index
Pattern
Sliding Window + last seen index
Java Code
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> map = new HashMap<>();
HashMap Page 1
HashMap<Character, Integer> map = new HashMap<>();
int left = 0, maxLen = 0;
for (int right = 0; right < [Link](); right++) {
char c = [Link](right);
if ([Link](c)) {
left = [Link](left, [Link](c) + 1);
}
[Link](c, right);
maxLen = [Link](maxLen, right - left + 1);
}
return maxLen;
}
Problem
Check if two strings are anagrams.
HashMap idea
Count frequency of characters.
Pattern
Frequency comparison
Java Code
public boolean isAnagram(String s, String t) {
if ([Link]() != [Link]()) return false;
HashMap<Character, Integer> map = new HashMap<>();
for (char c : [Link]())
[Link](c, [Link](c, 0) + 1);
for (char c : [Link]()) {
if () return false;
[Link](c, [Link](c) - 1);
if ([Link](c) == 0) [Link](c);
}
return [Link]();
}
Problem
Find first character that appears only once.
HashMap idea
Character frequency
Java Code
public int firstUniqChar(String s) {
HashMap<Character, Integer> map = new HashMap<>();
for (char c : [Link]())
[Link](c, [Link](c, 0) + 1);
for (int i = 0; i < [Link](); i++) {
if ([Link]([Link](i)) == 1)
return i;
}
return -1;
}
HashMap Page 2
Problem
Find element appearing more than n/2 times.
HashMap idea
Frequency count
Java Code
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
[Link](n, [Link](n, 0) + 1);
if ([Link](n) > [Link] / 2)
return n;
}
return -1;
}
Problem
Count subarrays whose sum equals k.
HashMap idea
Store prefix sums
Pattern
sum → frequency
Java Code
public int subarraySum(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
[Link](0, 1);
int sum = 0, count = 0;
for (int n : nums) {
sum += n;
count += [Link](sum - k, 0);
[Link](sum, [Link](sum, 0) + 1);
}
return count;
}
Problem
Group words that are anagrams.
HashMap idea
Sorted string → list of words
Java Code
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] arr = [Link]();
[Link](arr);
String key = new String(arr);
[Link](key, new ArrayList<>());
HashMap Page 3
[Link](key, new ArrayList<>());
[Link](key).add(s);
}
return new ArrayList<>([Link]());
}
Problem
Longest substring with at most k distinct chars.
HashMap idea
Character frequency in window
Java Code
public int lengthOfLongestSubstringKDistinct(String s, int k) {
HashMap<Character, Integer> map = new HashMap<>();
int left = 0, maxLen = 0;
for (int right = 0; right < [Link](); right++) {
[Link]([Link](right),
[Link]([Link](right), 0) + 1);
while ([Link]() > k) {
char l = [Link](left);
[Link](l, [Link](l) - 1);
if ([Link](l) == 0) [Link](l);
left++;
}
maxLen = [Link](maxLen, right - left + 1);
}
return maxLen;
}
Problem
Count frequency of words.
Java Code
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
[Link](word, [Link](word, 0) + 1);
}
Longest Consecutive Sequence
Problem
Find longest consecutive sequence.
HashMap idea
Use HashSet (HashMap concept)
Java Code
public int longestConsecutive(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int n : nums) [Link](n);
int longest = 0;
for (int n : set) {
if () {
int curr = n, count = 1;
HashMap Page 4
int curr = n, count = 1;
while ([Link](curr + 1)) {
curr++;
count++;
}
longest = [Link](longest, count);
}
}
return longest;
}
FINAL MEMORY RULE
If a problem asks:
• How many times? → Frequency map
• Seen before? → containsKey
• Index tracking? → value = index
• Window problems? → map + left/right
If you want next
I can:
• Give DAILY HashMap practice plan (Day 1–7)
• Convert these into step-by-step dry runs
• Give same problems but EASY → HARD order
Just tell me
From <[Link]
HashMap Page 5