Let function with safe calls in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run code with the let function and safe calls changes as the input size grows.
Specifically, how does using let with safe calls affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
val list: List = listOf("a", null, "b", "c", null)
list.forEach { item ->
item?.let {
println(it.uppercase())
}
}
This code goes through a list of strings that might be null. For each non-null item, it prints the uppercase version.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forEachloop goes through each item in the list once. - How many times: It runs exactly once for each element in the list, so if the list has
nitems, it runsntimes.
As the list gets bigger, the number of times the loop runs grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and possible prints |
| 100 | About 100 checks and possible prints |
| 1000 | About 1000 checks and possible prints |
Pattern observation: The work grows evenly as the list grows. Double the list, double the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the number of items in the list.
[X] Wrong: "Using let with safe calls makes the code slower by adding extra loops or checks that multiply the work."
[OK] Correct: The let function just runs once per non-null item inside the existing loop. It does not add extra loops, only a small check, so the overall work still grows linearly.
Understanding how safe calls and let affect performance helps you write clear and efficient Kotlin code. It shows you can reason about how your code scales with input size, a key skill in programming.
"What if we replaced forEach with a nested loop inside the let block? How would the time complexity change?"
Practice
nullableVar?.let { println(it) } do?Solution
Step 1: Understand safe call operator usage
The?.operator checks ifnullableVaris not null before proceeding.Step 2: Analyze the
Ifletfunction behaviornullableVaris not null,letexecutes the lambda, printing the value.Final Answer:
Prints the value ofnullableVaronly if it is not null. -> Option AQuick Check:
?.letruns code only if not null [OK]
- Thinking it prints null when variable is null
- Assuming it throws an exception on null
- Confusing let with always running code
name using let?Solution
Step 1: Identify safe call usage
To avoid null errors,?.letmust be used, not just.let.Step 2: Check lambda syntax correctness
The lambda must be enclosed in braces{ }and useitto refer to the value.Final Answer:
name?.let { println(it) } -> Option AQuick Check:
Safe call + let with braces = correct syntax [OK]
- Omitting the safe call operator (?.)
- Missing braces around lambda
- Using variable name inside lambda instead of 'it'
val number: Int? = 5
number?.let { println(it * 2) }Solution
Step 1: Check if number is null
The variablenumberholds 5, which is not null, so?.letruns.Step 2: Calculate inside let block
The lambda multipliesit(5) by 2 and prints 10.Final Answer:
10 -> Option BQuick Check:
Non-null value runs let, prints doubled value [OK]
- Expecting no output if value is not null
- Confusing printed value with original variable
- Thinking it prints null when variable is non-null
val text: String? = null
text.let { println(it.length) }Solution
Step 1: Check safe call usage
The code usestext.letwithout?., so it runs even iftextis null.Step 2: Understand null dereference
Inside the lambda,it.lengthis called on null, causing a NullPointerException.Final Answer:
It throws NullPointerException because safe call is missing. -> Option CQuick Check:
Missing ?. causes null error [OK]
- Assuming let skips null without safe call
- Thinking null acts like empty string
- Ignoring possibility of NullPointerException
val names: List<String>? = listOf("Anna", "Bob", null, "Cara"), which Kotlin code safely prints only non-null names using let with safe calls?Solution
Step 1: Handle nullable list safely
Usenames?.forEachto avoid errors ifnamesis null.Step 2: Filter out null elements inside the list
Inside the loop, useit?.letto print only non-null names.Final Answer:
names?.forEach { it?.let { println(it) } } -> Option DQuick Check:
Safe calls on list and elements print only non-null [OK]
- Not using safe call on list causing errors
- Printing null elements without filtering
- Using let without safe call on nullable elements
Ready to prove it?
Pass the test and this pattern moves unknown
