Extension properties in Kotlin - Time & Space Complexity
Extension properties in Kotlin let us add new properties to existing classes without changing them.
We want to see how the time to get these properties changes as input size grows.
Analyze the time complexity of the following code snippet.
val String.wordCount: Int
get() = this.split(" ").size
fun main() {
val sentence = "Kotlin extension properties are cool and useful"
println(sentence.wordCount)
}
This code adds a property to count words in a string by splitting it and counting parts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Splitting the string into words using
split(" "). - How many times: The split method scans each character once, so it repeats roughly once per character in the string.
As the string gets longer, the split operation checks more characters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The work grows directly with the number of characters in the string.
Time Complexity: O(n)
This means the time to get the extension property grows in a straight line with the string length.
[X] Wrong: "Getting an extension property is always instant and does not depend on input size."
[OK] Correct: Extension properties can run code like any function, so if they process data (like splitting a string), time depends on input size.
Understanding how extension properties work under the hood helps you explain performance clearly and shows you know Kotlin features well.
"What if the extension property cached the word count after the first call? How would the time complexity change on repeated calls?"
Practice
Solution
Step 1: Understand extension properties concept
Extension properties allow adding new properties to classes without changing their original code.Step 2: Identify correct description
"A property added to an existing class without modifying its source code" correctly describes this as adding properties externally without modifying the class.Final Answer:
A property added to an existing class without modifying its source code -> Option DQuick Check:
Extension property = added externally [OK]
- Thinking extension properties store data inside the class
- Confusing extension properties with member properties
- Believing extension properties can replace existing ones
isEven for Int in Kotlin?Solution
Step 1: Recall extension property syntax
Extension properties useval/var Type.propertyName get() = ...without a body for setter if read-only.Step 2: Check options
val Int.isEven get() = this % 2 == 0 correctly usesval Int.isEven get() = this % 2 == 0. var Int.isEven = this % 2 == 0 wrongly usesvarwithout setter. fun Int.isEven() = this % 2 == 0 is a function, not a property. val isEven: Int get() = this % 2 == 0 misses receiver type.Final Answer:
val Int.isEven get() = this % 2 == 0 -> Option BQuick Check:
Extension property syntax = val Type.prop get() [OK]
- Using var without setter for extension property
- Writing extension property as a function
- Omitting receiver type before property name
val String.firstChar: Char
get() = this[0]
fun main() {
val word = "hello"
println(word.firstChar)
}Solution
Step 1: Understand extension property behavior
The extension propertyfirstCharreturns the first character of the string usingthis[0].Step 2: Trace the code output
Forword = "hello",word.firstCharreturns 'h', which is printed.Final Answer:
h -> Option CQuick Check:
Extension property returns first char = h [OK]
- Expecting full string instead of first character
- Confusing property with function call
- Thinking extension properties cause errors
var String.lastChar: Char
get() = this[length - 1]
set(value) { this[length - 1] = value }Solution
Step 1: Review extension property setter rules
Extension properties can have setters, but they cannot modify the original object if it is immutable.Step 2: Check String mutability
Strings in Kotlin are immutable; you cannot assign tothis[length - 1]. This causes a compilation error.Final Answer:
You cannot assign to a character in a String -> Option AQuick Check:
Strings immutable = cannot set chars [OK]
- Assuming extension properties can store data
- Thinking extension setters always cause errors
- Confusing syntax errors with immutability issues
isAdult to the Person class that returns true if age is 18 or more, otherwise false. Given data class Person(val name: String, val age: Int), which is the correct extension property implementation?Solution
Step 1: Define read-only extension property
Sinceageis a val, we can only create a read-only extension property with a getter.Step 2: Check options for correct syntax and logic
val Person.isAdult get() = age >= 18 correctly definesval Person.isAdult get() = age >= 18. var Person.isAdult get() = age >= 18; set(value) { age = if(value) 18 else 0 } tries to setagewhich is immutable. fun Person.isAdult() = age >= 18 is a function, not a property. val isAdult: Person get() = age >= 18 has wrong syntax and receiver placement.Final Answer:
val Person.isAdult get() = age >= 18 -> Option AQuick Check:
Read-only extension property with getter = val Person.isAdult get() = age >= 18 [OK]
- Trying to create setter for immutable property
- Confusing extension property with extension function
- Incorrect syntax for receiver type in property
Ready to prove it?
Pass the test and this pattern moves unknown
