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 string keys?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { string[] words = {"eat", "tea", "tan", "ate", "nat", "bat"}; var map = new Dictionary<string, List<string>>(); foreach (var word in words) { var chars = word.ToCharArray(); Array.Sort(chars); string key = new string(chars); if (!map.ContainsKey(key)) { map[key] = new List<string>(); } map[key].Add(word); } foreach (var group in map.Values) { Console.WriteLine(string.Join(", ", group)); } } }
Attempts:
2 left
💡 Hint
Think about how sorting the characters of each word groups anagrams together.
✗ Incorrect
The code sorts each word's characters to create a key. Words with the same sorted key are anagrams and grouped together. The groups are: ["eat", "tea", "ate"], ["bat"], and ["tan", "nat"].
🧠 Conceptual
intermediateKey Choice in Grouping Anagrams
Why is sorting the characters of each word a good choice for the key in a hash map when grouping anagrams?
Attempts:
2 left
💡 Hint
Think about what makes anagrams similar and how sorting helps identify that.
✗ Incorrect
Anagrams have the same letters in different orders. Sorting letters puts them in a consistent order, so all anagrams share the same sorted key.
🔧 Debug
advancedIdentify the Error in Anagram Grouping Code
What error will this C# code produce when grouping anagrams using a hash map?
DSA C#
string[] words = {"bat", "tab", "eat"};
var map = new Dictionary<string, List<string>>();
foreach (var word in words) {
var chars = word.ToCharArray();
Array.Sort(chars);
string key = chars.ToString();
if (!map.ContainsKey(key)) {
map[key] = new List<string>();
}
map[key].Add(word);
}Attempts:
2 left
💡 Hint
Check what chars.ToString() returns in C#.
✗ Incorrect
chars.ToString() returns the type name "System.Char[]", not the string of characters. So all keys are the same, failing to group only anagrams.
🚀 Application
advancedNumber of Groups Formed by Grouping Anagrams
Given the list of words ["listen", "silent", "enlist", "google", "gooegl", "abc"], 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 characters and count unique groups.
✗ Incorrect
The groups are: ["listen", "silent", "enlist"], ["google", "gooegl"], and ["abc"]. So total 3 groups.
❓ Predict Output
expertOutput of Group Anagrams with Frequency Count Key
What is the output of this C# code that groups anagrams using a frequency count key instead of sorting?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { string[] words = {"abc", "bca", "cab", "xyz", "zyx", "zxy"}; var map = new Dictionary<string, List<string>>(); foreach (var word in words) { int[] freq = new int[26]; foreach (var ch in word) { freq[ch - 'a']++; } string key = string.Join("#", freq); if (!map.ContainsKey(key)) { map[key] = new List<string>(); } map[key].Add(word); } foreach (var group in map.Values) { Console.WriteLine(string.Join(", ", group)); } } }
Attempts:
2 left
💡 Hint
Frequency count keys represent the count of each letter, grouping anagrams together.
✗ Incorrect
Words with the same letter frequency form the same key, grouping all anagrams together. So two groups: ["abc", "bca", "cab"] and ["xyz", "zyx", "zxy"].
