Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a hash map for character counts.
DSA C++
std::unordered_map<char, int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid variable name.
Forgetting to specify the template parameters.
✗ Incorrect
We declare an unordered_map named charCount to store character frequencies.
2fill in blank
mediumComplete the code to increment the count of each character in the string.
DSA C++
for (char c : s) { [1][c]++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than declared.
Forgetting to increment the count.
✗ Incorrect
We use charCount[c]++ to increase the count of character c in the map.
3fill in blank
hardFix the error in the code to find the first non-repeating character.
DSA C++
for (char c : s) { if ([1][c] == 1) { return c; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name causing compilation error.
Checking for count != 1 instead of == 1.
✗ Incorrect
We check charCount[c] to find characters that appear exactly once.
4fill in blank
hardFill the blanks to complete the function that returns the first non-repeating character or '_' if none found.
DSA C++
char firstNonRepeatingChar(const std::string& s) {
std::unordered_map<char, int> [1];
for (char c : s) {
[2][c]++;
}
for (char c : s) {
if ([3][c] == 1) {
return c;
}
}
return '_';
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Forgetting to return '_' when no unique character is found.
✗ Incorrect
The same variable charCount is used for declaration and accessing counts.
5fill in blank
hardFill all three blanks to create a map comprehension-like code that counts characters and finds the first unique one.
DSA C++
std::unordered_map<char, int> [1]; for (char [2] : s) { [1][[2]]++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using undeclared variables.
✗ Incorrect
We declare charCount, iterate with variable c, and update charCount[c].
