Lambda creation and behavior in Ruby - Time & Space Complexity
We want to understand how creating and using lambdas affects the time it takes for a program to run.
Specifically, we ask: how does the number of operations grow when we create and call lambdas?
Analyze the time complexity of the following code snippet.
my_lambda = ->(n) { n * 2 }
10.times do |i|
puts my_lambda.call(i)
end
This code creates a lambda that doubles a number, then calls it 10 times, printing each result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the lambda inside a loop.
- How many times: The lambda is called once for each iteration of the loop (10 times here).
Each time we increase the number of calls, the total work grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to lambda |
| 100 | 100 calls to lambda |
| 1000 | 1000 calls to lambda |
Pattern observation: The total work grows directly with the number of times the lambda is called.
Time Complexity: O(n)
This means the time to run grows in a straight line as we call the lambda more times.
[X] Wrong: "Creating a lambda inside the loop is free and does not add to time."
[OK] Correct: Creating a lambda inside a loop means making a new function each time, which adds work and slows the program.
Understanding how lambdas behave helps you explain how your code runs and scales, a skill that shows you think about efficiency clearly.
"What if we created the lambda inside the loop instead of before it? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a lambda represents
A lambda is a reusable piece of code stored in a variable, like a mini-function.Step 2: Compare with other options
Options A, C, and D describe loops, methods, or variables, not lambdas.Final Answer:
A small block of code stored in a variable that can be called later -> Option DQuick Check:
Lambda = reusable code block [OK]
- Confusing lambdas with loops
- Thinking lambdas run automatically
- Believing lambdas hold only numbers
Solution
Step 1: Identify valid lambda syntax
In Ruby, lambdas can be created using the arrow syntax ->(params) { code } or lambda { |params| code }.Step 2: Check each option
lambda(x) { x * 2 }: invalid syntax (lambda keyword expects block with |params|, not parenthesized args). def lambda(x) x * 2 end: method definition, not a lambda. lambda(x) => x * 2: invalid syntax. ->(x) { x * 2 }: correct arrow syntax.Final Answer:
->(x) { x * 2 } -> Option AQuick Check:
Arrow syntax ->(x) { ... } creates lambda [OK]
- Using def instead of lambda
- Incorrect arrow syntax
- Confusing method and lambda syntax
my_lambda = ->(x) { x + 3 }
puts my_lambda.call(4)Solution
Step 1: Understand the lambda behavior
The lambda adds 3 to the input number x.Step 2: Calculate the output
Calling my_lambda.call(4) computes 4 + 3 = 7.Final Answer:
7 -> Option CQuick Check:
4 + 3 = 7 [OK]
- Forgetting to use .call to invoke lambda
- Confusing input and output values
- Expecting syntax error
my_lambda = lambda { |x, y| x + y }
puts my_lambda.call(5)Solution
Step 1: Check lambda argument requirements
Lambdas check arguments strictly. This lambda expects 2 arguments (x and y).Step 2: Analyze the call with one argument
Calling my_lambda.call(5) provides only 1 argument, causing ArgumentError.Final Answer:
It will raise an ArgumentError -> Option AQuick Check:
Lambda argument count mismatch = ArgumentError [OK]
- Assuming missing arguments default to nil
- Expecting no error on fewer arguments
- Confusing lambdas with procs (which are lenient)
def test_lambda
l = -> { return "Inside lambda" }
result = l.call
return "Outside lambda"
end
puts test_lambdaSolution
Step 1: Understand lambda return behavior
Return inside a lambda exits only the lambda, not the enclosing method.Step 2: Trace method execution
The lambda returns "Inside lambda" to variable result, but method continues and returns "Outside lambda".Final Answer:
Outside lambda -> Option BQuick Check:
Lambda return exits lambda only, method returns after [OK]
- Thinking return inside lambda exits method
- Expecting "Inside lambda" as output
- Confusing lambda with proc return behavior
Ready to prove it?
Pass the test and this pattern moves unknown
