Any type as universal base in Kotlin - Time & Space Complexity
When using Kotlin's Any type as a universal base, it's important to see how operations on these objects grow with input size.
We want to know how the program's work changes as we handle more Any objects.
Analyze the time complexity of the following code snippet.
fun printAll(items: List) {
for (item in items) {
println(item.toString())
}
}
This code prints the string form of each item in a list of Any objects.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list.
- How many times: Once for every item in the list.
As the list gets bigger, the program prints more items, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the list gets bigger.
[X] Wrong: "Using Any makes the code slower because it has to check types for every item."
[OK] Correct: The loop runs once per item regardless of type. The toString() call is a simple method call and does not add extra loops or checks that change the overall time growth.
Understanding how operations on universal types like Any scale helps you explain performance clearly and confidently in interviews.
"What if we changed the list to a nested list of Any objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand Kotlin's type hierarchy
Kotlin usesAnyas the root of its type system, similar toObjectin Java.Step 2: Identify the universal base type
Anycan hold any non-null value, making it the universal base type in Kotlin.Final Answer:
Any -> Option DQuick Check:
Universal base type = Any [OK]
- Confusing Any with Object (Java's base type)
- Thinking Base or Universal are Kotlin types
- Assuming Any can hold null (it cannot)
Any in Kotlin?Solution
Step 1: Review Kotlin variable declaration syntax
Variables are declared withvalorvar, followed by a colon and the type.Step 2: Check correct usage of
AnytypeAnyis a type, not a constructor, so you assign values directly without parentheses.Final Answer:
val x: Any = 10 -> Option AQuick Check:
Declare with colon and type name =val x: Any = 10[OK]
- Trying to call Any as a constructor
- Using lowercase 'any' instead of 'Any'
- Adding parentheses after type name
fun main() {
val a: Any = "Hello"
val b: Any = 123
println(a)
println(b)
}Solution
Step 1: Understand variable assignments
Variableaholds a string "Hello" andbholds an integer 123, both typed asAny.Step 2: Analyze the print statements
Printingaandbcalls theirtoString()method, outputting "Hello" and "123" respectively.Final Answer:
Hello 123 -> Option AQuick Check:
Any holds values, prints their string form [OK]
- Expecting type name 'Any' to print
- Thinking it causes compile error
- Assuming null prints for Any
fun main() {
val x: Any = null
println(x)
}Solution
Step 1: Check type nullability
Anyin Kotlin is non-nullable by default, so it cannot holdnull.Step 2: Identify assignment error
Assigningnulltoxof typeAnycauses a compile-time error.Final Answer:
Cannot assign null to a variable of type Any -> Option CQuick Check:
Any is non-nullable, null assignment fails [OK]
- Thinking println can't print Any
- Expecting semicolons in Kotlin
- Confusing val and var for nullability
Any as the base type?val mixedList: List<Any> = listOf(1, "two", 3.0, true)
Which Kotlin code correctly produces
List<String> with string forms of all elements?Solution
Step 1: Understand the goal
We want a list of strings representing all elements, not just the strings themselves.Step 2: Analyze each option
val stringList = mixedList.map { it.toString() }maps each element to its string form, producing List<String>.val stringList = mixedList.filterIsInstance<String>()filters only strings, losing other elements.val stringList = mixedList.map { it as String }tries unsafe cast causing runtime error.val stringList = mixedList.filter { it is String }filters strings but returns List<Any>, not List<String>.Final Answer:
val stringList = mixedList.map { it.toString() } -> Option BQuick Check:
Map all to string form for List<String> [OK]
- Using filterIsInstance<String> loses non-string elements
- Casting with 'as String' causes runtime errors
- Filtering returns List<Any>, not List<String>
Ready to prove it?
Pass the test and this pattern moves unknown
