Single-expression functions in Kotlin - Time & Space Complexity
We want to see how fast a single-expression function runs as the input size changes.
How does the work inside this short function grow when input grows?
Analyze the time complexity of the following code snippet.
fun sumList(numbers: List<Int>) = numbers.sum()
This function returns the sum of all numbers in a list using a single-expression style.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function calls
sum()which goes through each number in the list once. - How many times: It visits every item in the list exactly one time.
As the list gets bigger, the function does more work by adding more numbers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of the list.
[X] Wrong: "Because the function is one line, it must run instantly no matter the input size."
[OK] Correct: Even a single line can do a lot of work if it processes every item in a list. The length of the list matters more than the number of lines.
Understanding how simple-looking functions still depend on input size helps you explain your code clearly and think about efficiency in real projects.
"What if we changed the function to return the first item instead of summing all? How would the time complexity change?"
Practice
single-expression function in Kotlin?Solution
Step 1: Understand single-expression function syntax
Single-expression functions use an equals sign followed by an expression, without braces.Step 2: Identify the return behavior
They automatically return the value of that expression without needing an explicit return keyword.Final Answer:
It returns the value of the expression after the equals sign automatically. -> Option AQuick Check:
Single-expression function = auto return [OK]
- Thinking explicit return is needed
- Assuming parameters are not allowed
- Believing it must be inside a class
Solution
Step 1: Identify single-expression function syntax
It uses '=' followed by the expression without braces or explicit return.Step 2: Check each option
fun add(a: Int, b: Int) = a + b matches this syntax exactly:fun add(a: Int, b: Int) = a + b.Final Answer:
fun add(a: Int, b: Int) = a + b -> Option BQuick Check:
Equals sign and expression = correct syntax [OK]
- Using braces with return in single-expression functions
- Using '=>' instead of '='
- Omitting '=' sign
fun square(x: Int) = x * x println(square(4))
Solution
Step 1: Understand the function behavior
The functionsquarereturns the product ofxtimesx.Step 2: Calculate the output for input 4
4 * 4 = 16, sosquare(4)returns 16, which is printed.Final Answer:
16 -> Option AQuick Check:
4 squared = 16 [OK]
- Expecting the expression as a string output
- Confusing input with output
- Thinking it causes a compile error
fun greet(name: String) = {
"Hello, $name!"
}Solution
Step 1: Check single-expression function syntax
Single-expression functions use '=' followed by an expression without braces.Step 2: Identify the error
The braces after '=' create a lambda block, which is not allowed here; it should be just the expression.Final Answer:
Using braces {} after equals sign is incorrect. -> Option CQuick Check:
No braces allowed after '=' in single-expression functions [OK]
- Adding braces after '='
- Thinking return type is mandatory
- Misusing string interpolation syntax
Solution
Step 1: Understand the requirement
The function must return length if string is not empty, else zero, in a single expression.Step 2: Check each option
fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 uses a single-expression function with an if-else expression correctly. fun lengthOrZero(s: String) { if (s.isNotEmpty()) return s.length else return 0 } uses braces and explicit returns, not single-expression. fun lengthOrZero(s: String): Int = { if (s.isNotEmpty()) s.length else 0 } wrongly uses braces after '='. fun lengthOrZero(s: String) = s.length ?: 0 misuses the Elvis operator on Int.Final Answer:
fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 -> Option DQuick Check:
If-else expression after '=' = correct single-expression function [OK]
- Using braces after '='
- Trying to use return inside single-expression function
- Misusing Elvis operator on non-nullable Int
Ready to prove it?
Pass the test and this pattern moves unknown
