Object declaration syntax in Kotlin - Time & Space Complexity
Let's see how the time it takes to run code changes when we use Kotlin's object declaration syntax.
We want to know how the program's work grows as we create or use objects this way.
Analyze the time complexity of the following code snippet.
object Logger {
fun log(message: String) {
println(message)
}
}
fun main(n: Int) {
for (i in 1..n) {
Logger.log("Message $i")
}
}
This code defines a single object Logger and calls its log function n times in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling Logger.log inside a loop.
- How many times: Exactly n times, once per loop iteration.
Each time we increase n, the number of log calls grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log calls |
| 100 | 100 log calls |
| 1000 | 1000 log calls |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of times we call the log function.
[X] Wrong: "Since Logger is a single object, calling its method many times is constant time."
[OK] Correct: Even though the object is created once, each method call still takes time, so more calls mean more total time.
Understanding how repeated calls to a single object affect time helps you explain performance clearly and confidently in interviews.
"What if we created a new Logger object inside the loop each time instead of using the object declaration? How would the time complexity change?"
Practice
object keyword in Kotlin do?Solution
Step 1: Understand the
Theobjectkeyword purposeobjectkeyword creates a singleton, meaning one single instance with properties and functions.Step 2: Compare with other options
Classes can be instantiated multiple times, variables don't define singletons, and functions returning objects are different concepts.Final Answer:
Creates a single instance with properties and functions -> Option DQuick Check:
object keyword = single instance [OK]
- Confusing object with class
- Thinking object creates multiple instances
- Mixing object with variable declaration
Config in Kotlin?Solution
Step 1: Identify the object declaration syntax
The correct syntax to declare a singleton object is using theobjectkeyword followed by the name and curly braces.Step 2: Check other options
class Config { } declares a class, B declares an anonymous object assigned to a val, and D declares a function, so they are incorrect.Final Answer:
object Config { } -> Option AQuick Check:
object keyword + name + braces = correct syntax [OK]
- Using class instead of object
- Assigning anonymous object to val instead
- Using function syntax by mistake
object Logger {
fun log(msg: String) = println("Log: $msg")
}
fun main() {
Logger.log("Start")
}Solution
Step 1: Understand object usage
The object Logger has a function log that prints "Log: " plus the message.Step 2: Trace the main function call
Calling Logger.log("Start") prints "Log: Start" to the console.Final Answer:
Log: Start -> Option AQuick Check:
Logger.log("Start") prints "Log: Start" [OK]
- Expecting just the message without prefix
- Thinking it prints the function call text
- Assuming compilation error due to object usage
object Data {
var count: Int
fun increment() {
count++
}
}Solution
Step 1: Check property declaration
The property 'count' is declared but not initialized or marked as abstract, which is invalid in objects.Step 2: Validate other options
Objects can have vars, functions can be Unit-returning, and objects can have properties, so other options are incorrect.Final Answer:
Missing initialization for 'count' property -> Option BQuick Check:
Properties in objects must be initialized [OK]
- Forgetting to initialize properties
- Thinking vars are disallowed in objects
- Expecting functions to always return a value
Cache that stores key-value pairs and provides a function getValue(key: String): String?. Which of the following is the best Kotlin object declaration?Solution
Step 1: Identify singleton pattern
Usingobjectcreates a singleton Cache with a private mutable map and a function to get values.Step 2: Compare other options
class Cache { val map = mutableMapOf() fun getValue(key: String) = map[key] } is a class needing instances, C is a function returning a new map each time, D uses an immutable map and separate function, not encapsulated.Final Answer:
object Cache { private val map = mutableMapOf() fun getValue(key: String) = map[key] } -> Option CQuick Check:
Singleton object with private mutable map = object Cache { private val map = mutableMapOf() fun getValue(key: String) = map[key] } [OK]
- Using class instead of object for singleton
- Returning new map each call instead of storing
- Using immutable map when mutation is needed
Ready to prove it?
Pass the test and this pattern moves unknown
