Challenge - 5 Problems
Character Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of character frequency count using map
What is the output of this C++ code that counts character frequency in a string?
DSA C++
std::string s = "hello"; std::map<char, int> freq; for (char c : s) { freq[c]++; } for (auto &p : freq) { std::cout << p.first << ":" << p.second << " "; }
Attempts:
2 left
💡 Hint
Remember std::map sorts keys in ascending order.
✗ Incorrect
std::map stores keys sorted by default. Characters are sorted by ASCII codes: 'e'(101), 'h'(104), 'l'(108), 'o'(111). So output order is e, h, l, o.
❓ Predict Output
intermediateFrequency count using unordered_map output order
What is the output of this C++ code counting character frequency using unordered_map?
DSA C++
std::string s = "abcabc"; std::unordered_map<char, int> freq; for (char c : s) { freq[c]++; } for (auto &p : freq) { std::cout << p.first << ":" << p.second << " "; }
Attempts:
2 left
💡 Hint
unordered_map does not guarantee order of keys when iterating.
✗ Incorrect
unordered_map stores keys in hash buckets, so iteration order is not guaranteed and may differ between runs or platforms.
🔧 Debug
advancedIdentify error in character frequency counting code
What error does this C++ code produce when counting character frequency?
DSA C++
std::string s = "test"; std::map<char, int> freq; for (int i = 0; i < s.size(); i++) { freq[s[i]]++; }
Attempts:
2 left
💡 Hint
Check loop boundary conditions carefully.
✗ Incorrect
Loop uses i <= s.size(), so last iteration accesses s[s.size()] which is out-of-bounds causing undefined behavior.
🚀 Application
advancedFind character with highest frequency
Given a string, which code snippet correctly finds the character with the highest frequency?
Attempts:
2 left
💡 Hint
Track the maximum frequency and update character accordingly.
✗ Incorrect
Option C correctly updates maxFreq and maxChar when a higher frequency is found. Others either use wrong comparisons or overwrite without condition.
🧠 Conceptual
expertTime complexity of character frequency counting
What is the time complexity of counting character frequency in a string of length n using std::unordered_map in C++?
Attempts:
2 left
💡 Hint
Consider average and worst case behavior of hash tables.
✗ Incorrect
unordered_map provides average O(1) insert and access, so counting n characters is O(n) average. Worst case with many collisions can degrade to O(n^2).
