Type conversion is always explicit in Kotlin - Time & Space Complexity
We want to understand how the time cost changes when converting types explicitly in Kotlin.
How does the number of operations grow as input size increases during explicit type conversion?
Analyze the time complexity of the following code snippet.
fun convertList(numbers: List): List {
val result = mutableListOf()
for (num in numbers) {
result.add(num.toDouble())
}
return result
}
This code converts a list of integers to a list of doubles by explicitly converting each element.
- Primary operation: Looping through each element in the input list.
- How many times: Once for each element in the list (n times).
Each element requires one explicit conversion operation, so the total work grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 conversions |
| 100 | 100 conversions |
| 1000 | 1000 conversions |
Pattern observation: The number of operations increases in a straight line as input size grows.
Time Complexity: O(n)
This means the time to convert grows directly in proportion to the number of items you convert.
[X] Wrong: "Type conversion happens automatically and costs no extra time."
[OK] Correct: In Kotlin, you must convert types explicitly, and each conversion takes time proportional to the number of elements converted.
Understanding how explicit type conversions scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we used a sequence instead of a list for conversion? How would the time complexity change?"
Practice
toInt() or toDouble() to convert types?Solution
Step 1: Understand Kotlin's type safety
Kotlin is designed to be safe and clear, so it avoids implicit conversions that might cause bugs.Step 2: Recognize explicit conversion requirement
To convert types, Kotlin requires explicit calls liketoInt()ortoDouble()to make conversions clear and intentional.Final Answer:
Because Kotlin does not perform automatic type conversion to avoid unexpected errors. -> Option DQuick Check:
Explicit conversion = Kotlin safety [OK]
- Assuming Kotlin converts types automatically
- Thinking conversions happen only for mutable variables
- Believing conversions occur only at runtime
Double value 3.14 to an Int?Solution
Step 1: Check each option for valid Kotlin syntax
val num: Int = 3.14 tries implicit conversion, which Kotlin disallows. val num: Int = 3.14 as Int uses 'as' cast, which is invalid for primitive types. val num: Int = Int(3.14) tries to call a constructor like a function, which is invalid.Step 2: Identify the correct explicit conversion
val num: Int = 3.14.toInt() usestoInt(), the correct explicit function to convert a Double to Int in Kotlin.Final Answer:
val num: Int = 3.14.toInt() -> Option CQuick Check:
Use toInt() for Double to Int [OK]
- Using implicit assignment without conversion
- Using 'as' cast for primitive types
- Trying to call type as a function
val x: Double = 9.99 val y: Int = x.toInt() println(y)
Solution
Step 1: Understand the conversion from Double to Int
ThetoInt()function converts a Double to Int by dropping the decimal part, not rounding.Step 2: Apply conversion to the value 9.99
Dropping the decimal part of 9.99 results in 9.Final Answer:
9 -> Option BQuick Check:
toInt() drops decimals = 9 [OK]
- Assuming toInt() rounds the number
- Expecting a compilation error
- Confusing Double and Int print output
val a: Int = 5 val b: Double = a println(b)
Solution
Step 1: Understand Kotlin's type conversion rules
Kotlin does not allow implicit conversion from Int to Double, soval b: Double = acauses a compilation error.Step 2: Apply explicit conversion to fix the error
Usinga.toDouble()explicitly converts Int to Double, fixing the error.Final Answer:
Change val b: Double = a to val b: Double = a.toDouble() -> Option AQuick Check:
Explicit conversion needed for Int to Double [OK]
- Assuming implicit conversion works
- Using 'as' cast for primitive types
- Ignoring compilation errors
Double values: val numbers = listOf(1.5, 2.7, 3.9). You want to create a list of Int values by converting each Double to Int. Which Kotlin code correctly does this?Solution
Step 1: Understand the list transformation requirement
You want to convert each Double in the list to an Int explicitly.Step 2: Check each option for correctness
val ints = numbers.map { it } returns the original Doubles without conversion. val ints = numbers.map { it as Int } uses 'as' cast, invalid for primitive types. val ints = numbers.map { Int(it) } tries to call Int as a constructor, which is invalid. val ints = numbers.map { it.toInt() } correctly usestoInt()insidemapto convert each element.Final Answer:
val ints = numbers.map { it.toInt() } -> Option AQuick Check:
Use map with toInt() for list conversion [OK]
- Using map without conversion
- Using 'as' cast for primitives
- Trying to call Int as a function
Ready to prove it?
Pass the test and this pattern moves unknown
