Challenge - 5 Problems
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sorting-Based Anagram Check
What is the output of this C# code that checks if two strings are anagrams by sorting?
DSA C#
using System; using System.Linq; class Program { static void Main() { string s1 = "listen"; string s2 = "silent"; bool result = s1.OrderBy(c => c).SequenceEqual(s2.OrderBy(c => c)); Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Sorting both strings and comparing sequences checks if they have the same characters.
✗ Incorrect
The code sorts both strings and compares them. Since "listen" and "silent" have the same letters, the result is True.
❓ Predict Output
intermediateOutput of Frequency Count Anagram Check
What does this C# code print when checking if two strings are anagrams using character frequency counts?
DSA C#
using System; using System.Collections.Generic; class Program { static bool AreAnagrams(string s1, string s2) { if (s1.Length != s2.Length) return false; var counts = new Dictionary<char, int>(); foreach (char c in s1) { if (counts.ContainsKey(c)) { counts[c]++; } else { counts[c] = 1; } } foreach (char c in s2) { if (!counts.ContainsKey(c) || counts[c] == 0) return false; counts[c]--; } return true; } static void Main() { Console.WriteLine(AreAnagrams("triangle", "integral")); } }
Attempts:
2 left
💡 Hint
Counting characters in both strings and comparing counts confirms anagrams.
✗ Incorrect
Both strings have the same characters with the same frequency, so the function returns True.
❓ Predict Output
advancedOutput of Anagram Check with Case Sensitivity
What is the output of this C# code that checks if two strings are anagrams considering case sensitivity?
DSA C#
using System; using System.Linq; class Program { static void Main() { string s1 = "Dormitory"; string s2 = "dirtyroom"; bool result = s1.OrderBy(c => c).SequenceEqual(s2.OrderBy(c => c)); Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Uppercase and lowercase letters have different ASCII codes, affecting sorting.
✗ Incorrect
Because 'D' and 'd' are different characters, the sorted sequences differ, so the result is False.
❓ Predict Output
advancedOutput of Anagram Check Using LINQ GroupBy
What does this C# code print when checking if two strings are anagrams using LINQ GroupBy and counts?
DSA C#
using System; using System.Linq; class Program { static bool AreAnagrams(string s1, string s2) { var g1 = s1.GroupBy(c => c).OrderBy(g => g.Key).Select(g => g.Count()); var g2 = s2.GroupBy(c => c).OrderBy(g => g.Key).Select(g => g.Count()); return g1.SequenceEqual(g2); } static void Main() { Console.WriteLine(AreAnagrams("apple", "paper")); } }
Attempts:
2 left
💡 Hint
Grouping by character and comparing counts only ignores which characters they are.
✗ Incorrect
The code compares only counts of characters ignoring which characters they belong to, so it returns False for these strings.
🧠 Conceptual
expertMost Efficient Anagram Check for Large Unicode Strings
Which approach is best for checking anagrams of very large strings containing Unicode characters efficiently in C#?
Attempts:
2 left
💡 Hint
Consider Unicode range and memory efficiency for large inputs.
✗ Incorrect
Using a Dictionary handles all Unicode characters efficiently without sorting overhead or fixed-size limitations.
