Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a HashMap to store character counts.
DSA Java
Map<Character, Integer> countMap = new HashMap<>(); for (char ch : str.toCharArray()) { countMap.[1](ch, countMap.getOrDefault(ch, 0) + 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() instead of put() to update the map.
Trying to remove keys instead of updating counts.
✗ Incorrect
We use put() to add or update the count of each character in the HashMap.
2fill in blank
mediumComplete the code to check if a character count is 1 (non-repeating).
DSA Java
for (char ch : str.toCharArray()) { if (countMap.get(ch) [1] 1) { return ch; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == which checks for non-equality.
Using > or < which do not specifically check for count 1.
✗ Incorrect
We check if the count is exactly 1 to find the first non-repeating character.
3fill in blank
hardFix the error in the code to return a default value when no non-repeating character is found.
DSA Java
return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null which is invalid for char return type.
Returning space ' ' which is a valid character.
✗ Incorrect
Returning '\0' (null character) is a common way to indicate no valid character found.
4fill in blank
hardFill both blanks to complete the method signature and initialize the HashMap.
DSA Java
public static char [1](String str) { Map<Character, Integer> [2] = new HashMap<>();
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic or unclear method names.
Using variable names that do not indicate their purpose.
✗ Incorrect
The method is named firstNonRepeatingChar and the map variable is countMap.
5fill in blank
hardFill all three blanks to complete the loop that updates counts and finds the first non-repeating character.
DSA Java
for (char ch : str.toCharArray()) { countMap.[1](ch, countMap.getOrDefault(ch, 0) [2] 1); } for (char ch : str.toCharArray()) { if (countMap.get(ch) [3] 1) { return ch; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() instead of put() to update counts.
Using subtraction or wrong comparison operators.
✗ Incorrect
We use put() to update counts, add 1 to the current count, and check if count equals 1.
