Bird
Raised Fist0
DSA C#programming~10 mins

Group Anagrams Using Hash Map in DSA C# - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a dictionary that groups anagrams by their sorted key.

DSA C#
var map = new Dictionary<string, List<string>>();
foreach (var word in words) {
    var key = new string(word.ToCharArray()[1]);
    if (!map.ContainsKey(key)) {
        map[key] = new List<string>();
    }
    map[key].Add(word);
}
Drag options to blanks, or click blank then click option'
A.Distinct().ToArray()
B.Reverse()
C.Sort()
D.OrderBy(c => c).ToArray()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .Sort() directly on the array without converting to string.
Not sorting the characters before creating the key.
2fill in blank
medium

Complete the code to return the grouped anagrams as a list of lists.

DSA C#
return map.Values.[1](list => list.ToList()).ToList();
Drag options to blanks, or click blank then click option'
ASelect
BWhere
COrderBy
DGroupBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using Where instead of Select.
Trying to use GroupBy again on the values.
3fill in blank
hard

Fix the error in the code that tries to add a word to the dictionary list.

DSA C#
if (!map.ContainsKey(key)) {
    map[key] = new List<string>();
}
map.[1](key).Add(word);
Drag options to blanks, or click blank then click option'
ARemove
BGetValueOrDefault
CAdd
DTryGetValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add method on dictionary which expects a key-value pair.
Using Remove which deletes entries.
4fill in blank
hard

Fill both blanks to correctly sort characters and add words to the dictionary.

DSA C#
foreach (var word in words) {
    var key = new string(word.ToCharArray()[1]);
    if (!map.[2](key)) {
        map[key] = new List<string>();
    }
    map[key].Add(word);
}
Drag options to blanks, or click blank then click option'
A.OrderBy(c => c).ToArray()
BContainsKey
CContainsValue
D.Reverse()
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsValue instead of ContainsKey.
Using Reverse instead of sorting characters.
5fill in blank
hard

Fill all three blanks to create the grouped anagrams dictionary and return the result.

DSA C#
var map = new Dictionary<string, List<string>>();
foreach (var word in words) {
    var key = new string(word.ToCharArray()[1]);
    if (!map.[2](key)) {
        map[key] = new List<string>();
    }
    map[key].Add(word);
}
return map.Values.[3](list => list.ToList()).ToList();
Drag options to blanks, or click blank then click option'
A.OrderBy(c => c).ToArray()
BContainsKey
CSelect
DWhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using Where instead of Select for projection.
Using ContainsValue instead of ContainsKey.
Not sorting characters before creating the key.

Practice

(1/5)
1. What is the main idea behind grouping anagrams using a hash map?
easy
A. Use sorted strings as keys to group words with the same letters
B. Use the length of the word as the key to group anagrams
C. Use the first letter of the word as the key to group anagrams
D. Use the last letter of the word as the key to group anagrams

Solution

  1. Step 1: Understand anagrams

    Anagrams are words with the same letters in different orders.
  2. Step 2: Use sorted string as key

    Sorting letters of each word gives a unique key for all anagrams.
  3. Final Answer:

    Use sorted strings as keys to group words with the same letters -> Option A
  4. Quick Check:

    Sorted string key groups anagrams [OK]
Hint: Sort each word and use it as key in hash map [OK]
Common Mistakes:
  • Using word length as key causes wrong grouping
  • Using first or last letter misses many anagrams
  • Not sorting letters before using as key
2. Which of the following C# code snippets correctly initializes a hash map to group anagrams?
easy
A. var map = new Dictionary();
B. var map = new List>();
C. var map = new Dictionary();
D. var map = new Dictionary();

Solution

  1. Step 1: Identify correct dictionary type

    We need keys as strings (sorted words) and values as lists of strings (anagrams).
  2. Step 2: Check each option

    var map = new Dictionary(); uses Dictionary which fits the requirement.
  3. Final Answer:

    var map = new Dictionary(); -> Option A
  4. Quick Check:

    Dictionary with string keys and list values [OK]
Hint: Use Dictionary for grouping anagrams [OK]
Common Mistakes:
  • Using List of dictionaries instead of dictionary
  • Using wrong key or value types
  • Using Dictionary which can't group anagrams
3. What is the output of the following C# code?
var strs = new string[] {"eat", "tea", "tan", "ate", "nat", "bat"};
var map = new Dictionary();
foreach (var s in strs) {
  var key = String.Concat(s.OrderBy(c => c));
  if (!map.ContainsKey(key)) map[key] = new List<string>();
  map[key].Add(s);
}
foreach (var group in map.Values) {
  Console.WriteLine(string.Join(", ", group));
}
medium
A. "eat, tea\nate, tan\nbat, nat"
B. "eat, tan, bat\ntea, ate, nat"
C. "eat, tea, ate\ntan, nat\nbat"
D. "eat, tea, ate, tan, nat, bat"

Solution

  1. Step 1: Sort each word to find keys

    "eat", "tea", "ate" all sort to "aet"; "tan", "nat" to "ant"; "bat" to "abt".
  2. Step 2: Group words by keys and print

    Groups are ["eat", "tea", "ate"], ["tan", "nat"], ["bat"]. Printed line by line.
  3. Final Answer:

    "eat, tea, ate\ntan, nat\nbat" -> Option C
  4. Quick Check:

    Sorted keys group anagrams correctly [OK]
Hint: Sort words, group by sorted key, print groups [OK]
Common Mistakes:
  • Printing all words in one line
  • Grouping by unsorted words
  • Mixing groups incorrectly
4. Identify the bug in this C# code snippet for grouping anagrams:
var map = new Dictionary();
foreach (var s in strs) {
  var key = s.OrderBy(c => c).ToString();
  if (!map.ContainsKey(key)) map[key] = new List<string>();
  map[key].Add(s);
}
medium
A. Not checking if key exists before adding
B. Missing initialization of map dictionary
C. Using List<string> instead of List<char> for values
D. Using ToString() on IEnumerable<char> does not produce sorted string key

Solution

  1. Step 1: Understand s.OrderBy(c => c)

    This returns IEnumerable<char>, not a string.
  2. Step 2: Using ToString() on IEnumerable<char>

    ToString() returns type name, not sorted string key. Need to join chars to string.
  3. Final Answer:

    Using ToString() on IEnumerable<char> does not produce sorted string key -> Option D
  4. Quick Check:

    Use String.Concat or new string() to convert chars to string [OK]
Hint: Convert sorted chars to string with String.Concat, not ToString() [OK]
Common Mistakes:
  • Calling ToString() on IEnumerable<char> expecting string
  • Not initializing dictionary before use
  • Ignoring key existence check
5. Given a list of strings, how would you modify the grouping anagrams code to ignore case differences (e.g., "Eat" and "tea" should be grouped)?
hard
A. Group by string length only
B. Convert each string to lowercase before sorting and using as key
C. Sort strings but keep original case for keys
D. Use the original string as key without sorting

Solution

  1. Step 1: Normalize case for grouping

    Convert each string to lowercase to treat "Eat" and "tea" the same.
  2. Step 2: Sort the lowercase string and use as key

    Sorting lowercase letters groups anagrams ignoring case.
  3. Final Answer:

    Convert each string to lowercase before sorting and using as key -> Option B
  4. Quick Check:

    Lowercase + sort = case-insensitive grouping [OK]
Hint: Lowercase strings before sorting for case-insensitive grouping [OK]
Common Mistakes:
  • Sorting without case normalization
  • Using original case keys causing separate groups
  • Grouping by length which is insufficient
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard