Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new HashMap for counting characters.
DSA Java
Map<Character, Integer> freqMap = new [1]<>();Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a map for counting frequencies.
Choosing 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 get the character at index i from the string.
DSA Java
char ch = input.[1](i);Attempts:
3 left
💡 Hint
Common Mistakes
Using substring which returns a string, not a char.
Using indexOf which finds the position of a character.
✗ Incorrect
charAt(i) returns the character at position i in the string.
3fill in blank
hardFix the error in updating the frequency count for the character.
DSA Java
freqMap.put(ch, freqMap.getOrDefault(ch, [1]) + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using null causes NullPointerException.
Starting count at 1 causes double counting.
✗ Incorrect
getOrDefault returns 0 if the character is not yet in the map, so we can add 1 safely.
4fill in blank
hardFill both blanks to print each character and its frequency.
DSA Java
for (Map.Entry<Character, Integer> entry : freqMap.[1]()) { System.out.println(entry.[2]() + " -> " + entry.getValue()); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using keySet() only gives keys, not entries.
getEntry() is not a valid method.
✗ Incorrect
entrySet() returns all key-value pairs; getKey() gets the character key.
5fill in blank
hardFill all three blanks to create a frequency map using streams.
DSA Java
Map<Character, Long> freqMap = input.chars()
.mapToObj(c -> (char) [1])
.collect(Collectors.[2](c -> c, Collectors.[3]()));Attempts:
3 left
💡 Hint
Common Mistakes
Using toMap instead of groupingBy causes errors.
Not casting int to char results in wrong keys.
✗ Incorrect
We cast int to char with (char) c, group by character, and count occurrences.
