Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a HashMap to count frequencies of characters.
DSA Java
Map<Character, Integer> freqMap = new [1]<>();Attempts:
3 left
💡 Hint
Common Mistakes
Using a List instead of a Map.
Using a Set which does not store counts.
✗ Incorrect
We use HashMap to store character keys and their frequency counts.
2fill in blank
mediumComplete the code to update the frequency count for a character c.
DSA Java
freqMap.put(c, freqMap.getOrDefault(c, [1]) + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as default causes counts to be off by one.
Using null causes runtime errors.
✗ Incorrect
If the character is not in the map, start count at 0 before adding 1.
3fill in blank
hardFix the error in the code to check if two strings have the same character frequency.
DSA Java
if (freqMap1.size() != freqMap2.[1]()) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or length() which are not methods of HashMap.
Using count which does not exist.
✗ Incorrect
HashMap uses size() method to get the number of keys.
4fill in blank
hardFill both blanks to complete the loop that compares frequency maps.
DSA Java
for (Character key : freqMap1.[1]()) { if (!freqMap2.[2](key) || !freqMap2.get(key).equals(freqMap1.get(key))) { return false; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using containsValue() instead of containsKey().
Using entrySet() incorrectly for keys.
✗ Incorrect
Use keySet() to get all keys and containsKey() to check if key exists in second map.
5fill in blank
hardFill all three blanks to create a frequency map from a string s.
DSA Java
Map<Character, Integer> freqMap = new HashMap<>(); for (int [1] = 0; [2] < s.length(); [3]++) { char c = s.charAt(i); freqMap.put(c, freqMap.getOrDefault(c, 0) + 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Forgetting to increment the loop variable.
✗ Incorrect
Use variable i for loop index consistently in initialization, condition, and increment.
