Collection size and emptiness checks in Kotlin - Time & Space Complexity
When working with collections, checking if they are empty or finding their size is common.
We want to know how the time it takes to do these checks changes as the collection grows.
Analyze the time complexity of the following code snippet.
fun checkCollection(collection: Collection<Any>) {
if (collection.isEmpty()) {
println("Collection is empty")
} else {
println("Collection size is ${collection.size}")
}
}
This code checks if a collection is empty and then prints its size if not empty.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking if the collection is empty and getting its size.
- How many times: Each check happens once per function call.
Checking emptiness or size usually takes the same time no matter how big the collection is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays about the same even if the collection grows larger.
Time Complexity: O(1)
This means checking if a collection is empty or getting its size takes the same short time no matter how many items it holds.
[X] Wrong: "Checking size or emptiness takes longer as the collection grows."
[OK] Correct: Most collections keep track of their size internally, so these checks do not need to count items each time.
Understanding that size and emptiness checks are quick helps you write efficient code and explain your choices clearly in interviews.
"What if the collection was a linked list without a stored size? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of
This function returns true if the collection has zero elements.isEmpty()Step 2: Compare with other functions
isNotEmpty()checks for one or more elements,count()andsizereturn the number of elements but do not return a boolean.Final Answer:
isEmpty()-> Option DQuick Check:
Check empty collection withisEmpty()= true [OK]
isEmpty() to check for no elements [OK]- Confusing
isEmpty()withisNotEmpty() - Using
count()to check emptiness - Calling
size()as a function instead of property
items is not empty in Kotlin?Solution
Step 1: Identify correct function call syntax
In Kotlin,isNotEmpty()is a function and requires parentheses.Step 2: Check other options for syntax errors
isEmptywithout parentheses is invalid,sizeis a property, not a function, sosize()is wrong, andcountis a function, socount > 0is invalid.Final Answer:
if (items.isNotEmpty()) { ... } -> Option AQuick Check:
UseisNotEmpty()with parentheses [OK]
isNotEmpty() needs parentheses [OK]- Using property syntax without parentheses for functions
- Calling
sizeas a function - Using
countwithout parentheses
val numbers = listOf(1, 2, 3, 4) println(numbers.size) println(numbers.isEmpty()) println(numbers.isNotEmpty())
Solution
Step 1: Check the size of the list
The list has 4 elements, sonumbers.sizereturns 4.Step 2: Evaluate emptiness checks
numbers.isEmpty()returns false because the list is not empty.numbers.isNotEmpty()returns true because the list has elements.Final Answer:
4 false true -> Option CQuick Check:
Size = 4, empty? false, not empty? true [OK]
- Thinking size is zero-based index
- Confusing isEmpty() and isNotEmpty() results
- Assuming size is a function, causing error
val data = listOf<String>()
if (data.isEmpty) {
println("No data")
}Solution
Step 1: Check usage of isEmpty
isEmptyis a function and must be called with parentheses:isEmpty().Step 2: Verify other parts of the code
listOf<String>()creates an empty list correctly,printlnsyntax is correct, and mutability is not required for checking emptiness.Final Answer:
Missing parentheses after isEmpty -> Option AQuick Check:
Functions need parentheses even if no arguments [OK]
- Using property syntax for functions
- Thinking empty list creation is invalid
- Assuming mutability is needed for checks
Solution
Step 1: Check condition for non-empty list
Usingusers.size > 0correctly checks if the list has one or more elements.Step 2: Verify printing statement
The print statement uses string interpolation to show the number of users online.Step 3: Analyze incorrect options
isEmpty()checks for empty, so if (users.isEmpty()) println("Online users: ${users.size}") prints when empty, which is wrong.countis a function, socount == 0is invalid.isNotEmptywithout parentheses is incorrect syntax.Final Answer:
if (users.size > 0) println("Online users: ${users.size}") -> Option BQuick Check:
Check size > 0 for non-empty list [OK]
- Using isEmpty() to check for non-empty
- Calling count without parentheses
- Omitting parentheses on isNotEmpty()
Ready to prove it?
Pass the test and this pattern moves unknown
