Lambda expression syntax in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run code with lambda expressions changes as the input grows.
Specifically, how does using a lambda affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
var numbers = new List<int> {1, 2, 3, 4, 5};
var squares = numbers.Select(x => x * x).ToList();
This code creates a list of numbers and uses a lambda expression to create a new list of their squares.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The lambda runs once for each number in the list.
- How many times: Exactly as many times as there are items in the list.
Each number causes one multiplication operation inside the lambda.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the list gets bigger.
[X] Wrong: "Using a lambda makes the code run slower in a way that changes the time complexity."
[OK] Correct: The lambda is just a short way to write a function. It runs once per item, so it does not add extra loops or steps beyond the list size.
Understanding how lambdas work with collections helps you explain code efficiency clearly and confidently in interviews.
"What if the lambda expression called another method that itself loops over the list? How would the time complexity change?"
Practice
x => x * 2 represent in C#?Solution
Step 1: Understand lambda syntax
The expressionx => x * 2means a function taking inputxand returningx * 2.Step 2: Interpret the operation
It doubles the input value by multiplying it by 2.Final Answer:
A function that doubles the input value -> Option BQuick Check:
Lambdax => x * 2doubles input [OK]
- Thinking lambda is a variable
- Confusing lambda with loops
- Assuming lambda is a conditional
Solution
Step 1: Check parameter syntax
Parameters must be enclosed in parentheses when there is more than one, so(a, b)is correct.Step 2: Check expression body
The expressiona + bwithout braces is valid for single expressions. (a, b) => a + b matches this.Final Answer:
(a, b) => a + b -> Option AQuick Check:
Two parameters need parentheses and expression body [OK]
- Omitting parentheses for multiple parameters
- Adding semicolon inside lambda expression
- Using braces without return keyword incorrectly
Func<int, int> square = x => x * x; Console.WriteLine(square(4));
Solution
Step 1: Understand the lambda function
The lambdax => x * xreturns the square of the input number.Step 2: Calculate the output for input 4
4 * 4 = 16, sosquare(4)returns 16.Final Answer:
16 -> Option DQuick Check:
Square of 4 is 16 [OK]
- Confusing multiplication with addition
- Expecting lambda to print instead of return
- Thinking lambda causes syntax error
Func<int, int> add = (x, y) => x + y;
Solution
Step 1: Check Func delegate signature
Func<int, int>expects one input parameter and one output (int).Step 2: Compare lambda parameters
The lambda has two parameters(x, y), which does not match the delegate expecting one.Final Answer:
Lambda has too many parameters for Func<int, int> -> Option CQuick Check:
Func<int, int> means one input, one output [OK]
- Assuming multiple parameters allowed for Func
- Forgetting delegate parameter count rules
- Thinking return keyword is always needed
List<int>.Where()?var numbers = new List<int> {1, 2, 3, 4, 5, 6};
var odds = numbers.Where( /* which lambda? */ );Solution
Step 1: Understand filtering condition
We want to keep odd numbers, so condition isx % 2 == 1.Step 2: Check lambda syntax and logic
x => x % 2 == 1 uses correct syntax and logic to filter odds. x => x % 2 = 1 uses assignment (=) which is invalid. x => { return x % 2 == 0; } filters evens, not odds. x => x / 2 == 1 checks division, not oddness.Final Answer:
x => x % 2 == 1 -> Option AQuick Check:
Filter odds with modulo == 1 [OK]
- Using = instead of == in lambda
- Filtering evens instead of odds
- Confusing division with modulo
Ready to prove it?
Pass the test and this pattern moves unknown
