Named companion objects in Kotlin - Time & Space Complexity
Let's explore how the time it takes to run code with named companion objects changes as the program grows.
We want to see how the number of operations changes when using named companion objects in Kotlin.
Analyze the time complexity of the following code snippet.
class Example {
companion object Factory {
fun createList(n: Int): List {
val list = mutableListOf()
for (i in 1..n) {
list.add(i)
}
return list
}
}
}
fun main() {
val numbers = Example.Factory.createList(1000)
}
This code defines a named companion object Factory inside a class Example. It creates a list of numbers from 1 to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds numbers to the list.
- How many times: It runs exactly n times, once for each number from 1 to n.
As n grows, the loop runs more times, so the work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: Doubling n doubles the work because each number is added once.
Time Complexity: O(n)
This means the time to create the list grows directly with the number of items you want to add.
[X] Wrong: "Using a named companion object makes the code slower because it adds extra steps."
[OK] Correct: The companion object is just a way to organize code. It does not add loops or repeated work, so it does not affect how the time grows with input size.
Understanding how companion objects work and their impact on performance shows you know how Kotlin organizes code and how that relates to efficiency.
"What if the createList function used recursion instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand companion objects
Companion objects are singletons inside a class used to hold members related to the class itself.Step 2: Purpose of naming companion objects
Giving a name to a companion object helps to clearly identify and access its members, improving code readability.Final Answer:
To give a clear and specific name to the singleton object inside a class -> Option AQuick Check:
Named companion object = clear singleton name [OK]
- Thinking companion objects create multiple instances
- Confusing naming with inheritance
- Assuming naming restricts access
Solution
Step 1: Recall correct companion object syntax
The correct syntax is 'companion object Name { ... }' inside the class.Step 2: Check each option
class MyClass { companion object Named { fun greet() = "Hi" } } matches the correct syntax exactly. Others have misplaced keywords or wrong order.Final Answer:
class MyClass { companion object Named { fun greet() = "Hi" } } -> Option CQuick Check:
companion object Name { } syntax [OK]
- Swapping 'object' and 'companion' keywords
- Writing 'companionobject' as one word
- Placing the name before 'companion'
class Box {
companion object Factory {
fun create() = "Box created"
}
}
fun main() {
println(Box.Factory.create())
}Solution
Step 1: Understand named companion object usage
The companion object named 'Factory' holds the function 'create' which returns "Box created".Step 2: Analyze the main function call
Calling 'Box.Factory.create()' accesses the named companion object and calls 'create', printing "Box created".Final Answer:
Box created -> Option BQuick Check:
Box.Factory.create() prints "Box created" [OK]
- Trying to call create() without Factory prefix
- Expecting 'Factory created' output
- Confusing syntax causing compile errors
class Sample {
companion object Companion {
fun info() = "Info"
}
}
fun main() {
println(Sample.companion.info())
}Solution
Step 1: Check companion object name and usage
The companion object is named 'Companion' with uppercase C.Step 2: Analyze the call in main()
The code calls 'Sample.companion.info()' with lowercase 'companion', which does not match the name and causes a compile error.Final Answer:
Incorrect companion object name usage; should be 'Companion' not 'companion' -> Option AQuick Check:
Use exact companion object name when calling [OK]
- Using lowercase 'companion' instead of named object
- Forgetting 'object' keyword
- Assuming companion functions must be static
Logger with a named companion object Factory that provides a method create() returning a new Logger instance. Which code correctly implements this?Solution
Step 1: Recall correct syntax for named companion object
The correct syntax is 'companion object Factory { ... }' inside the class.Step 2: Check method implementation
The method 'create()' returns a new instance of Logger using 'Logger()'. class Logger { companion object Factory { fun create() = Logger() } } matches both syntax and logic.Final Answer:
class Logger { companion object Factory { fun create() = Logger() } } -> Option DQuick Check:
Named companion object with factory method = class Logger { companion object Factory { fun create() = Logger() } } [OK]
- Swapping 'object' and 'companion' keywords
- Omitting the companion object name
- Incorrect order of keywords
Ready to prove it?
Pass the test and this pattern moves unknown
