If as an expression returning value in Kotlin - Time & Space Complexity
Let's see how using an if-expression that returns a value affects the time it takes to run code.
We want to know how the work grows when the input changes.
Analyze the time complexity of the following code snippet.
fun maxOfTwo(a: Int, b: Int): Int {
val max = if (a > b) a else b
return max
}
This code picks the bigger of two numbers using if as an expression that returns a value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single comparison between two numbers.
- How many times: Exactly once per function call.
Since the function compares only two numbers, the work stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 | 1 comparison |
| 10 | 1 comparison |
| 1000 | 1 comparison |
Pattern observation: The number of operations does not grow with input size.
Time Complexity: O(1)
This means the time to run the code stays the same no matter the input size.
[X] Wrong: "Using if as an expression makes the code slower because it returns a value."
[OK] Correct: The if-expression just chooses between two values once, so it does not add extra work compared to a normal if statement.
Understanding how simple expressions like if returning a value affect time helps you explain your code clearly and confidently in interviews.
"What if we used if-expression inside a loop that runs n times? How would the time complexity change?"
Practice
if is used as an expression in Kotlin?Solution
Step 1: Understand
In Kotlin,ifas expression conceptifcan return a value, unlike some other languages.Step 2: Recognize usage in assignments
This means you can assign the result of anifexpression directly to a variable.Final Answer:
It returns a value that can be assigned to a variable. -> Option AQuick Check:
ifreturns value = D [OK]
- Thinking if cannot return values
- Forgetting to include else block
- Assuming if is only a statement
if expression to a variable in Kotlin?Solution
Step 1: Check correct Kotlin
The correct syntax usesifexpression syntaxif (condition) value1 else value2inside an assignment.Step 2: Identify errors in other options
val result if (x > 0) = "Positive" else "Non-positive" has wrong order, C misseselse, D uses invalid keywords.Final Answer:
val result = if (x > 0) "Positive" else "Non-positive" -> Option BQuick Check:
Correct syntax with else = A [OK]
- Omitting else block
- Wrong keyword order
- Missing parentheses around condition
val x = 5 val result = if (x % 2 == 0) "Even" else "Odd" println(result)
Solution
Step 1: Evaluate the condition
Since 5 % 2 equals 1, the condition is false.x % 2 == 0Step 2: Determine which branch executes
Because the condition is false, theelsebranch returns "Odd".Final Answer:
Odd -> Option CQuick Check:
5 is odd = C [OK]
- Assuming 5 is even
- Ignoring else branch
- Confusing modulo operation
if as an expression:val x = 10 val result = if (x > 5) "Big"
Solution
Step 1: Check if expression completeness
When usingifas an expression, Kotlin requires anelsebranch to cover all cases.Step 2: Identify missing else branch
The code lacks anelsebranch, causing a compilation error.Final Answer:
Missing else branch for the if expression -> Option DQuick Check:
if expression needs else = B [OK]
- Omitting else block
- Assuming if can be incomplete
- Ignoring compiler errors
if as an expression. Which code correctly implements this?Solution
Step 1: Understand nested if as expression
Kotlin allows nestedifexpressions to return values in a single expression.Step 2: Check each option for correctness
fun checkNumber(n: Int): String = if (n > 0) "Positive" else if (n < 0) "Negative" else "Zero" correctly uses nestedifexpressions with proper syntax and returns all cases. fun checkNumber(n: Int): String { if (n > 0) return "Positive" else if (n < 0) return "Negative" else return "Zero" } uses statements, not expression style. fun checkNumber(n: Int): String = if (n > 0) "Positive" else "Negative" misses the zero case. fun checkNumber(n: Int): String = if n > 0 then "Positive" else if n < 0 then "Negative" else "Zero" uses invalid syntax.Final Answer:
fun checkNumber(n: Int): String = if (n > 0) "Positive" else if (n < 0) "Negative" else "Zero" -> Option AQuick Check:
Nested if expression with else = A [OK]
- Missing else for zero case
- Using statement style instead of expression
- Incorrect syntax with then keyword
Ready to prove it?
Pass the test and this pattern moves unknown
