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 JavaScript code that groups anagrams using a hash map with sorted strings as keys?
DSA Javascript
function groupAnagrams(strs) {
const map = new Map();
for (const str of strs) {
const sorted = str.split('').sort().join('');
if (!map.has(sorted)) {
map.set(sorted, []);
}
map.get(sorted).push(str);
}
return Array.from(map.values());
}
const input = ["eat", "tea", "tan", "ate", "nat", "bat"];
console.log(groupAnagrams(input));Attempts:
2 left
💡 Hint
Think about how sorting each string groups anagrams together as the same key.
✗ Incorrect
The code sorts each string to create a key. Anagrams have the same sorted key, so they are grouped together in the map. The final output is an array of these grouped anagrams.
🧠 Conceptual
intermediateWhy Use Sorted Strings as Keys in Grouping Anagrams?
Why do we use the sorted version of each string as the key in the hash map when grouping anagrams?
Attempts:
2 left
💡 Hint
Think about what anagrams share in common after sorting.
✗ Incorrect
Anagrams have the same letters in different orders. Sorting puts letters in order, so all anagrams become the same sorted string. This makes it a perfect key to group them.
🔧 Debug
advancedIdentify the Error in Grouping Anagrams Code
What error does the following code produce when grouping anagrams using a hash map?
function groupAnagrams(strs) {
const map = {};
for (const str of strs) {
const sorted = str.split('').sort().join('');
if (!map[sorted]) {
map[sorted] = str;
} else {
map[sorted].push(str);
}
}
return Object.values(map);
}
const input = ["bat", "tab"];
console.log(groupAnagrams(input));
Attempts:
2 left
💡 Hint
Check what type map[sorted] is initialized as before push is called.
✗ Incorrect
map[sorted] is initialized as a string (str), not an array. Calling push on a string causes a TypeError.
❓ Predict Output
advancedOutput of Group Anagrams Using Character Count as Key
What is the output of this JavaScript code that groups anagrams using character count as the key in a hash map?
DSA Javascript
function groupAnagrams(strs) {
const map = new Map();
for (const str of strs) {
const count = new Array(26).fill(0);
for (const char of str) {
count[char.charCodeAt(0) - 97]++;
}
const key = count.join('#');
if (!map.has(key)) {
map.set(key, []);
}
map.get(key).push(str);
}
return Array.from(map.values());
}
const input = ["abc", "bca", "cab", "xyz", "zyx", "zxy"];
console.log(groupAnagrams(input));Attempts:
2 left
💡 Hint
Character count arrays uniquely identify anagram groups.
✗ Incorrect
The code counts characters for each string and uses the counts as keys. Anagrams have identical counts, so they group together. The output groups the three "abc" anagrams and the three "xyz" anagrams.
🧠 Conceptual
expertWhy Use Character Count Instead of Sorting for Grouping Anagrams?
What is the main advantage of using character count arrays as keys instead of sorting strings when grouping anagrams?
Attempts:
2 left
💡 Hint
Consider the time complexity of sorting vs counting characters.
✗ Incorrect
Sorting each string takes O(k log k) time (k = string length). Counting characters takes O(k) time. For very long strings, counting is faster and more efficient.
