Challenge - 5 Problems
Anagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of grouping anagrams?
Given the Rust code below that groups anagrams from a list of words, what is the printed output?
DSA Rust
use std::collections::HashMap; fn group_anagrams(words: Vec<&str>) -> Vec<Vec<String>> { let mut map: HashMap<String, Vec<String>> = HashMap::new(); for word in words { let mut chars: Vec<char> = word.chars().collect(); chars.sort_unstable(); let key = chars.into_iter().collect::<String>(); map.entry(key).or_default().push(word.to_string()); } map.into_values().collect() } fn main() { let input = vec!["eat", "tea", "tan", "ate", "nat", "bat"]; let groups = group_anagrams(input); println!("{:?}", groups); }
Attempts:
2 left
💡 Hint
Rust's HashMap preserves insertion order of keys since Rust 1.56, so groups appear in order of first insertion of each key.
✗ Incorrect
The code groups words by their sorted character key. Words with the same sorted characters are grouped together. The order of groups follows HashMap insertion order ('aet', 'ant', 'abt'). Option D shows this valid grouping.
🧠 Conceptual
intermediateWhy use sorted strings as keys in grouping anagrams?
In the context of grouping anagrams using a hash map, why do we use the sorted version of each word as the key?
Attempts:
2 left
💡 Hint
Think about what makes anagrams similar and how to represent that similarity as a key.
✗ Incorrect
Sorting the characters of a word produces a canonical form that is identical for all anagrams. This allows grouping all anagrams under the same key in the hash map.
🔧 Debug
advancedIdentify the runtime error in this anagram grouping code
What runtime error will this Rust code produce when grouping anagrams, and why?
```rust
use std::collections::HashMap;
fn group_anagrams(words: Vec<&str>) -> Vec> {
let mut map: HashMap> = HashMap::new();
for word in words {
let key = word.to_string();
let mut chars: Vec = key.chars().collect();
chars.sort_unstable();
map.entry(key).or_default().push(word.to_string());
}
map.into_values().collect()
}
```
Attempts:
2 left
💡 Hint
Check how the key is created and used for grouping.
✗ Incorrect
The key used in the hash map is the original word string, not the sorted string. This means anagrams have different keys and won't be grouped together, causing a logic error but no runtime panic.
📝 Syntax
advancedWhich option fixes the syntax error in this Rust anagram grouping snippet?
The following Rust code snippet has a syntax error. Which option fixes it?
```rust
let mut map: HashMap> = HashMap::new()
for word in words {
let mut chars = word.chars().collect::>();
chars.sort_unstable();
let key = chars.into_iter().collect::();
map.entry(key).or_default().push(word.to_string());
}
```
Attempts:
2 left
💡 Hint
Look carefully at the line endings and Rust syntax rules.
✗ Incorrect
The missing semicolon after `HashMap::new()` causes a syntax error. Adding it fixes the error. Other options do not fix the syntax error.
🚀 Application
expertHow many groups are formed by this anagram grouping code?
Given the input list `["listen", "silent", "enlist", "google", "gogole", "abc", "cab", "bac", "foo"]`, how many groups will the Rust anagram grouping code produce?
Attempts:
2 left
💡 Hint
Group words by their sorted characters and count unique groups.
✗ Incorrect
The groups are: ["listen", "silent", "enlist"], ["google", "gogole"], ["abc", "cab", "bac"], and ["foo"]. Total 4 groups.
