Challenge - 5 Problems
GroupBy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of GroupBy with simple key selector
What is the output of this C# code snippet that groups numbers by their remainder when divided by 3?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { var numbers = new[] {1, 2, 3, 4, 5, 6}; var groups = numbers.GroupBy(n => n % 3); foreach (var group in groups) { Console.WriteLine($"Key: {group.Key}, Values: {string.Join(",", group)}"); } } }
Attempts:
2 left
💡 Hint
Remember that GroupBy preserves the order of groups as they first appear in the source.
✗ Incorrect
GroupBy preserves the order of groups based on the first occurrence of each key in the source sequence: key 1 (from 1), key 2 (from 2), key 0 (from 3). Within each group, elements appear in source order: 1 then 4, etc.
❓ Predict Output
intermediate2:00remaining
GroupBy with complex key and projection
What is the output of this C# code that groups words by their first letter and selects the count of words in each group?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { string[] words = {"apple", "apricot", "banana", "blueberry", "cherry"}; var result = words.GroupBy(w => w[0]) .Select(g => new { Letter = g.Key, Count = g.Count() }); foreach (var item in result) { Console.WriteLine($"{item.Letter}: {item.Count}"); } } }
Attempts:
2 left
💡 Hint
Count how many words start with each letter.
✗ Incorrect
Words starting with 'a' are "apple" and "apricot" (2 words), with 'b' are "banana" and "blueberry" (2 words), and with 'c' is "cherry" (1 word). So counts are a:2, b:2, c:1.
🔧 Debug
advanced2:00remaining
Identify the error in GroupBy usage
What error does this C# code produce when run?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { var data = new[] {1, 2, 3, 4}; var groups = data.GroupBy(n => n > 2 ? "High" : "Low"); foreach (var group in groups) { Console.WriteLine(group.Key + ": " + group); } } }
Attempts:
2 left
💡 Hint
Check what happens when you print a group object directly.
✗ Incorrect
The code prints group.Key plus group.ToString(). The group.ToString() returns the type name, not the elements. So output shows keys and the type name of the group object, not the values.
❓ Predict Output
advanced2:00remaining
GroupBy with ordering inside groups
What is the output of this C# code that groups people by age and orders names inside each group alphabetically?
C Sharp (C#)
using System; using System.Linq; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { var people = new List<Person> { new Person { Name = "John", Age = 30 }, new Person { Name = "Jane", Age = 25 }, new Person { Name = "Jake", Age = 30 }, new Person { Name = "Jill", Age = 25 } }; var groups = people.GroupBy(p => p.Age) .Select(g => new { Age = g.Key, Names = g.OrderBy(p => p.Name).Select(p => p.Name) }); foreach (var group in groups) { Console.WriteLine($"Age: {group.Age}, Names: {string.Join(",", group.Names)}"); } } }
Attempts:
2 left
💡 Hint
Groups are ordered by first occurrence of keys, and names inside groups are alphabetically ordered.
✗ Incorrect
People order: John(30), Jane(25), Jake(30), Jill(25). First key 30, then 25. Names alpha sorted: 30: Jake,John; 25: Jane,Jill.
❓ Predict Output
expert3:00remaining
GroupBy with nested grouping and flattening
What is the output of this C# code that groups numbers by even/odd, then groups each group by remainder mod 3, and finally flattens the results?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { var numbers = Enumerable.Range(1, 6); var nestedGroups = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd") .Select(g => new { Parity = g.Key, SubGroups = g.GroupBy(x => x % 3) }); foreach (var group in nestedGroups) { Console.WriteLine(group.Parity + ":"); foreach (var subGroup in group.SubGroups) { Console.WriteLine($" Remainder {subGroup.Key}: {string.Join(",", subGroup)}"); } } } }
Attempts:
2 left
💡 Hint
Group numbers by even/odd, then inside each group by remainder mod 3, watch carefully the numbers in each subgroup.
✗ Incorrect
Odd numbers 1,3,5: rem 1,0,2 → subgroups order 1:1, 0:3, 2:5. Even 2,4,6: rem 2,1,0 → subgroups order 2:2, 1:4, 0:6. Matches B.