Star projection for unknown types in Kotlin - Time & Space Complexity
When using star projections in Kotlin generics, it's important to understand how the program's work changes as input grows.
We want to see how the unknown type affects the number of operations the program does.
Analyze the time complexity of the following code snippet.
fun printListSize(list: List<*>) {
println("Size: ${list.size}")
for (item in list) {
println(item)
}
}
fun main() {
val numbers: List = listOf(1, 2, 3, 4, 5)
printListSize(numbers)
}
This code prints the size and all elements of a list with an unknown type using star projection.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the list.
- How many times: Once for each element in the list (n times).
As the list gets bigger, the program prints more items, so the work grows with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prints plus 1 size check |
| 100 | About 100 prints plus 1 size check |
| 1000 | About 1000 prints plus 1 size check |
Pattern observation: The number of operations grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to run grows linearly with the number of items in the list.
[X] Wrong: "Using star projection makes the code slower or more complex."
[OK] Correct: Star projection only hides the exact type but does not add extra loops or slow down the code. The main work still depends on the list size.
Understanding how unknown types affect performance helps you write clear and efficient generic code, a useful skill in many coding tasks.
"What if we changed the list to a nested list like List<List<*>>? How would the time complexity change?"
Practice
* represent in Kotlin generics?Solution
Step 1: Understand star projection meaning
The star projection*means an unknown generic type in Kotlin.Step 2: Analyze its usage
It allows safe reading from generic collections but restricts writing to avoid type errors.Final Answer:
An unknown generic type that allows safe reading but restricts writing -> Option DQuick Check:
Star projection = unknown generic type with safe read [OK]
- Thinking star projection allows writing to generics
- Confusing star projection with specific generic types
- Assuming star projection only works with primitives
Solution
Step 1: Recall star projection syntax
Star projection uses*inside angle brackets to represent unknown type.Step 2: Check each option
val list: List<*> = listOf(1, 2, 3) usesList<*>which is correct syntax for star projection.Final Answer:
val list: List<*> = listOf(1, 2, 3) -> Option BQuick Check:
Star projection syntax = List<*> [OK]
- Leaving angle brackets empty like List<>
- Using List<Any> which is not star projection
- Confusing star projection with out-projection
fun printFirst(list: List<*>) {
println(list[0])
}
fun main() {
val numbers: List<Int> = listOf(10, 20, 30)
printFirst(numbers)
}Solution
Step 1: Understand star projection usage in function
The function acceptsList<*>, meaning unknown generic type, safe to read elements.Step 2: Analyze the call with List<Int>
PassingList<Int>is valid; readinglist[0]returns 10 safely.Final Answer:
10 -> Option AQuick Check:
Reading from List<*> returns element safely [OK]
- Assuming star projection causes compile error
- Expecting null instead of actual element
- Thinking runtime exception occurs on read
fun addElement(list: MutableList<*>, element: Any) {
list.add(element)
}Solution
Step 1: Understand star projection with MutableList
MutableList<*> means unknown generic type; writing is restricted to avoid type errors.Step 2: Analyze the add operation
Trying to addelementcauses compile error because type is unknown and unsafe to write.Final Answer:
Cannot add element to MutableList<*> due to unknown type -> Option CQuick Check:
Writing to MutableList<*> causes error [OK]
- Thinking add works with MutableList<*>
- Confusing syntax error with type error
- Assuming Any type is always accepted
List<*> to a MutableList<Any> in Kotlin?Solution
Step 1: Understand star projection restrictions
List<*> allows safe reading but elements are of unknown type, so direct assignment to MutableList<Any> is unsafe.Step 2: Safely copy elements
Use a loop to read each element and cast to Any before adding to MutableList<Any>, ensuring type safety.Final Answer:
Use a loop to cast each element to Any and add to MutableList<Any> -> Option AQuick Check:
Copy with loop and cast = safe transfer [OK]
- Trying direct assignment between incompatible generic types
- Using star projection on mutable list incorrectly
- Ignoring type casting when copying elements
Ready to prove it?
Pass the test and this pattern moves unknown
