0
0
C Sharp (C#)programming~20 mins

SelectMany for flattening in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SelectMany Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this SelectMany example?
Consider the following C# code using LINQ's SelectMany method. What will be printed to the console?
C Sharp (C#)
var numbers = new List<List<int>> {
    new List<int> {1, 2},
    new List<int> {3, 4},
    new List<int> {5}
};

var flat = numbers.SelectMany(x => x);
foreach(var num in flat)
{
    Console.Write(num + " ");
}
A1 3 5
BSystem.Collections.Generic.IEnumerable`1[System.Int32]
C1 2 3 4 5
D1 2 3 4
Attempts:
2 left
💡 Hint
SelectMany flattens nested collections into one sequence.
Predict Output
intermediate
2:00remaining
What does this SelectMany with index produce?
Look at this C# code using SelectMany with an index parameter. What is the output?
C Sharp (C#)
var data = new List<string[]> {
    new string[] {"a", "b"},
    new string[] {"c"},
    new string[] {"d", "e", "f"}
};

var result = data.SelectMany((arr, i) => arr.Select(s => s + i));
foreach(var s in result)
{
    Console.Write(s + " ");
}
Aa0 b0 c1 d2 e2 f2
Ba b c d e f
Ca1 b1 c2 d3 e4 f5
Da0 b1 c2 d3 e4 f5
Attempts:
2 left
💡 Hint
The index i is the position of the outer array in the list.
🔧 Debug
advanced
2:00remaining
Why does this SelectMany code throw an exception?
This code throws an exception at runtime. What is the cause?
C Sharp (C#)
var list = new List<List<int>> {
    new List<int> {1, 2},
    null,
    new List<int> {3}
};

var flat = list.SelectMany(x => x);
foreach(var n in flat)
{
    Console.Write(n + " ");
}
AOutput: 1 2 3
BInvalidOperationException because SelectMany expects non-empty lists
CCompile-time error due to null in list
DNullReferenceException because one inner list is null
Attempts:
2 left
💡 Hint
Check what happens when SelectMany tries to iterate a null collection.
📝 Syntax
advanced
2:00remaining
Which option correctly uses SelectMany to flatten and filter?
You want to flatten a list of integer arrays and keep only even numbers. Which code snippet does this correctly?
Avar result = list.SelectMany(x => x.Filter(n => n % 2 == 0));
Bvar result = list.SelectMany(x => x.Where(n => n % 2 == 0));
Cvar result = list.SelectMany(x => x.Select(n => n % 2 == 0));
Dvar result = list.SelectMany(x => n => n % 2 == 0);
Attempts:
2 left
💡 Hint
Use Where inside SelectMany to filter elements.
🚀 Application
expert
2:00remaining
How many items are in the flattened list?
Given this nested list, how many integers will be in the flattened list after using SelectMany?
C Sharp (C#)
var nested = new List<List<int>> {
    new List<int> {1, 2, 3},
    new List<int> {},
    new List<int> {4, 5},
    new List<int> {6}
};

var flat = nested.SelectMany(x => x).ToList();
int count = flat.Count;
A6
B5
C7
D4
Attempts:
2 left
💡 Hint
Count all elements in all inner lists combined.