Bird
Raised Fist0
DSA C#programming~20 mins

Group Anagrams Using Hash Map in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Anagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
        }
    }
}
A
eat, tea, ate
bat
tan, nat
B
eat, tea, ate
bat, tan, nat
C
eat, tea, ate, bat
tan, nat
D
eat, tea
ate, bat
tan, nat
Attempts:
2 left
💡 Hint
Think about how sorting the characters of each word groups anagrams together.
🧠 Conceptual
intermediate
1:30remaining
Key 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?
ABecause sorting characters makes the word shorter and easier to store.
BBecause sorted characters preserve the original word order for output.
CBecause sorted characters create a unique representation for all anagrams of a word.
DBecause sorting characters removes duplicate letters from the word.
Attempts:
2 left
💡 Hint
Think about what makes anagrams similar and how sorting helps identify that.
🔧 Debug
advanced
2:00remaining
Identify 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);
}
AThe code throws a NullReferenceException at runtime.
BThe code groups all words under the same "System.Char[]" key because chars.ToString() does not return the sorted string.
CThe code throws a compile-time error due to invalid dictionary usage.
DThe code runs correctly and groups anagrams properly.
Attempts:
2 left
💡 Hint
Check what chars.ToString() returns in C#.
🚀 Application
advanced
1:30remaining
Number 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?
A2
B5
C4
D3
Attempts:
2 left
💡 Hint
Group words by their sorted characters and count unique groups.
❓ Predict Output
expert
2:30remaining
Output 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));
        }
    }
}
A
abc, bca, cab
xyz, zyx, zxy
B
abc, bca
cab, xyz, zyx, zxy
C
abc
bca
cab
xyz
zyx
zxy
Dabc, bca, cab, xyz, zyx, zxy
Attempts:
2 left
💡 Hint
Frequency count keys represent the count of each letter, grouping anagrams together.

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