Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a map that counts character frequencies.
DSA C++
std::map<char, int> freq[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] or parentheses () instead of empty braces {} for map initialization.
Using angle brackets <> which are for template parameters.
✗ Incorrect
The map is declared with empty braces {} to default construct it using uniform initialization.
2fill in blank
mediumComplete the code to increase the frequency count of character c in the map.
DSA C++
freq[1]++;Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of square brackets [] for map access.
Using braces {} or angle brackets <> which are invalid here.
✗ Incorrect
Square brackets are used to access or insert elements in a map by key.
3fill in blank
hardFix the error in the loop to iterate over the map entries correctly.
DSA C++
for (auto [1] : freq) { std::cout << [1].first << " -> " << [1].second << std::endl; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one used inside the loop body.
Using reserved keywords as variable names.
✗ Incorrect
The loop variable name can be any valid identifier; 'entry' is clear and used here.
4fill in blank
hardFill both blanks to check if character c is in the map and print its frequency.
DSA C++
if (freq.find([1]) != freq.[2]()) { std::cout << c << ": " << freq[c] << std::endl; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using freq.begin() instead of freq.end() for comparison.
Using freq.count() in place of find() incorrectly.
✗ Incorrect
Use freq.find(c) to search and compare with freq.end() to check presence.
5fill in blank
hardFill all three blanks to create a map of character frequencies from string s.
DSA C++
std::map<char, int> freq; for (char [1] : [2]) { freq[3]++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access map elements.
Using wrong variable names inconsistent with the loop.
✗ Incorrect
Loop variable is ch, loop over string s, access map with freq[ch].
