Challenge - 5 Problems
LINQ Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
LINQ Deferred Execution Output
What will be the output of this C# code snippet using LINQ deferred execution?
C Sharp (C#)
var numbers = new List<int> {1, 2, 3, 4}; var query = numbers.Where(n => n > 2); numbers.Add(5); foreach (var num in query) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Remember that LINQ queries are not executed until you iterate over them.
✗ Incorrect
LINQ uses deferred execution, so the query runs when iterated. Since 5 was added before iteration, it is included.
🧠 Conceptual
intermediate1:30remaining
LINQ Query Performance Impact
Which of the following statements about LINQ query performance is TRUE?
Attempts:
2 left
💡 Hint
Think about how deferred execution affects memory and processing.
✗ Incorrect
Deferred execution means LINQ processes elements as needed, which can reduce memory usage compared to immediate execution.
❓ Predict Output
advanced2:30remaining
LINQ Query Performance with Multiple Enumerations
What is the output of this C# code and what performance issue does it illustrate?
C Sharp (C#)
var data = Enumerable.Range(1, 3); var query = data.Select(x => { Console.WriteLine($"Processing {x}"); return x * 2; }); var list1 = query.ToList(); var list2 = query.ToList(); Console.WriteLine(list1.Sum());
Attempts:
2 left
💡 Hint
Consider how many times the query is enumerated and what side effects happen during enumeration.
✗ Incorrect
The query is enumerated twice because ToList() is called twice, causing the processing code to run twice.
🔧 Debug
advanced2:00remaining
Identifying LINQ Performance Bottleneck
This code runs slower than expected. What is the main cause of the performance issue?
C Sharp (C#)
var largeList = Enumerable.Range(1, 1000000).ToList(); var result = largeList.Where(x => x % 2 == 0).OrderByDescending(x => x).Take(10).ToList();
Attempts:
2 left
💡 Hint
Think about how OrderByDescending works on large collections.
✗ Incorrect
Ordering the entire filtered list sorts all matching items, which is expensive. Better to limit items before sorting if possible.
🧠 Conceptual
expert3:00remaining
Best Practice for LINQ Performance Optimization
Which approach is the best practice to improve LINQ query performance on large datasets?
Attempts:
2 left
💡 Hint
Consider how deferred execution and multiple enumerations affect performance.
✗ Incorrect
Deferred execution is efficient but repeated enumeration can hurt performance; caching results avoids this.