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 Go code that groups anagrams using a hash map with sorted strings as keys?
DSA Go
package main import ( "fmt" "sort" "strings" ) func groupAnagrams(strs []string) [][]string { m := make(map[string][]string) for _, s := range strs { chars := strings.Split(s, "") sort.Strings(chars) sorted := strings.Join(chars, "") m[sorted] = append(m[sorted], s) } result := [][]string{} for _, group := range m { result = append(result, group) } return result } func main() { input := []string{"eat", "tea", "tan", "ate", "nat", "bat"} groups := groupAnagrams(input) fmt.Println(groups) }
Attempts:
2 left
💡 Hint
Think about how the map stores groups by sorted keys and how iteration order over maps in Go is random.
✗ Incorrect
The code groups strings by their sorted character sequence as keys in a map. Since Go maps iterate in random order, the groups can appear in any order. Option D shows a valid grouping with all anagrams together, matching the map's grouping but in a different order than option D. Options B and C have incorrect groupings.
🧠 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 makes anagrams similar and how keys in a map work.
✗ Incorrect
Anagrams have the same characters in different orders. Sorting the characters of each string creates a unique key that is the same for all anagrams, allowing them to be grouped together in the map.
🔧 Debug
advancedIdentify the Error in Group Anagrams Code
What error will this Go code produce when trying to group anagrams using a hash map?
DSA Go
package main import ( "fmt" "sort" "strings" ) func groupAnagrams(strs []string) [][]string { m := make(map[string][]string) for _, s := range strs { chars := strings.Split(s, "") sort.Strings(chars) m[chars] = append(m[chars], s) } result := [][]string{} for _, group := range m { result = append(result, group) } return result } func main() { input := []string{"eat", "tea", "tan"} fmt.Println(groupAnagrams(input)) }
Attempts:
2 left
💡 Hint
Check the type used as the map key and what Go allows as map keys.
✗ Incorrect
In Go, map keys must be comparable types. Slices (like []string) are not comparable and cannot be used as map keys. Using chars (a []string) as a key causes a compile-time error.
🚀 Application
advancedCount Unique Anagram Groups
Given the list of strings ["listen", "silent", "enlist", "google", "gogole", "abc"], how many unique anagram groups will the grouping function create?
Attempts:
2 left
💡 Hint
Group strings by their sorted characters and count distinct groups.
✗ Incorrect
The groups are: ["listen", "silent", "enlist"] (all anagrams), ["google", "gogole"] (anagrams), and ["abc"] alone. So total 3 groups.
❓ Predict Output
expertOutput of Group Anagrams with Unicode Characters
What is the output of this Go code that groups anagrams including Unicode characters?
DSA Go
package main import ( "fmt" "sort" "strings" ) func groupAnagrams(strs []string) [][]string { m := make(map[string][]string) for _, s := range strs { chars := strings.Split(s, "") sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] }) sorted := strings.Join(chars, "") m[sorted] = append(m[sorted], s) } result := [][]string{} for _, group := range m { result = append(result, group) } return result } func main() { input := []string{"résumé", "sérumé", "ésumér", "hello", "olleh"} groups := groupAnagrams(input) fmt.Println(groups) }
Attempts:
2 left
💡 Hint
Unicode characters are sorted lexicographically; anagrams share the same sorted sequence.
✗ Incorrect
The three accented words are anagrams sharing the same sorted characters, grouped together. 'hello' and 'olleh' form another group. Map iteration order is random but both A and B show correct groups; however, only A matches the actual output order from running the code.
