Challenge - 5 Problems
Anagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Grouping Anagrams by Sorted Key
What is the output of the following C++ code that groups anagrams using a hash map with sorted strings as keys?
DSA C++
#include <iostream> #include <vector> #include <string> #include <unordered_map> #include <algorithm> int main() { std::vector<std::string> words = {"eat", "tea", "tan", "ate", "nat", "bat"}; std::unordered_map<std::string, std::vector<std::string>> groups; for (auto& word : words) { std::string key = word; std::sort(key.begin(), key.end()); groups[key].push_back(word); } for (auto& pair : groups) { std::cout << "["; for (auto& w : pair.second) { std::cout << w << ", "; } std::cout << "]\n"; } return 0; }
Attempts:
2 left
💡 Hint
Focus on how the sorted key groups the anagrams together.
✗ Incorrect
The code sorts each word to create a key. All anagrams share the same sorted key, so they group together. The output order depends on the unordered_map iteration order, but the groups contain the correct anagrams.
🧠 Conceptual
intermediateWhy Use Sorted Strings as Keys in Grouping Anagrams?
Why do we use the sorted version of a string as the key in a hash map to group anagrams?
Attempts:
2 left
💡 Hint
Think about what makes anagrams similar.
✗ Incorrect
Anagrams have the same characters in different orders. Sorting puts characters in a fixed order, so all anagrams become the same sorted string, which can be used as a unique key.
🔧 Debug
advancedIdentify the Error in Grouping Anagrams Code
What error will this C++ code produce when grouping anagrams using a hash map?
DSA C++
#include <iostream> #include <vector> #include <string> #include <unordered_map> #include <algorithm> int main() { std::vector<std::string> words = {"bat", "tab", "eat"}; std::unordered_map<std::string, std::vector<std::string>> groups; for (auto word : words) { std::string key = word; std::sort(word.begin(), word.end()); groups[word].push_back(word); } for (auto& pair : groups) { std::cout << pair.first << ": "; for (auto& w : pair.second) { std::cout << w << " "; } std::cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Check what is stored as keys and values after sorting.
✗ Incorrect
The code sorts 'word' and uses it both as key and value, so original words are lost. The groups contain sorted words, not original words, causing logical error in output.
🚀 Application
advancedNumber of Groups Formed by Grouping Anagrams
Given the list of words: ["listen", "silent", "enlist", "google", "gooegl", "abc", "cab"], how many groups will be formed when grouping anagrams using a hash map with sorted keys?
Attempts:
2 left
💡 Hint
Group words by their sorted character sequences.
✗ Incorrect
Groups are: [listen, silent, enlist], [google, gooegl], [abc, cab]. So total 3 groups.
❓ Predict Output
expertOutput of Group Anagrams with Frequency Count Key
What is the output of the following C++ code that groups anagrams using a frequency count array as the key in a hash map?
DSA C++
#include <iostream> #include <vector> #include <string> #include <unordered_map> #include <array> struct Key { std::array<int, 26> count{}; bool operator==(const Key& other) const { return count == other.count; } }; namespace std { template<> struct hash<Key> { size_t operator()(const Key& k) const { size_t h = 0; for (int c : k.count) { h = h * 31 + c; } return h; } }; } int main() { std::vector<std::string> words = {"abc", "bca", "cab", "xyz", "zyx", "zxy", "aabb"}; std::unordered_map<Key, std::vector<std::string>> groups; for (auto& word : words) { Key k; for (char ch : word) { k.count[ch - 'a']++; } groups[k].push_back(word); } for (auto& pair : groups) { std::cout << "["; for (auto& w : pair.second) { std::cout << w << ", "; } std::cout << "]\n"; } return 0; }
Attempts:
2 left
💡 Hint
Frequency count keys group anagrams by character counts.
✗ Incorrect
Words with same character counts form groups. The groups are: [abc, bca, cab], [xyz, zyx, zxy], and [aabb]. The order depends on unordered_map iteration but matches option A.
