Sequence operators (map, filter) in Kotlin - Time & Space Complexity
We want to understand how long it takes to process a list using sequence operators like map and filter.
How does the time grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers
.filter { it % 2 == 0 }
.map { it * 2 }
This code filters even numbers from a list and then doubles each filtered number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The filter and map each go through the list once.
- How many times: Each element is checked once in filter, then each filtered element is processed once in map.
As the list size grows, the number of operations grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (10 for filter + up to 10 for map) |
| 100 | About 200 (100 for filter + up to 100 for map) |
| 1000 | About 2000 (1000 for filter + up to 1000 for map) |
Pattern observation: The total work grows roughly twice the input size, so it grows linearly.
Time Complexity: O(n)
This means the time to finish grows directly in proportion to the list size.
[X] Wrong: "Using both filter and map means the time grows like n squared because there are two steps."
[OK] Correct: Each step goes through the list once, so the total time adds up, not multiplies, keeping growth linear.
Understanding how sequence operations scale helps you write efficient code and explain your choices clearly in interviews.
"What if we chained three sequence operations instead of two? How would the time complexity change?"
Practice
map operator do in a Kotlin sequence?Solution
Step 1: Understand the purpose of
Themapmapoperator applies a function to each item, changing it to a new value.Step 2: Compare with other operators
filterremoves items,sortorders items, andcountcounts items, so these are different.Final Answer:
It transforms each item in the sequence to a new value. -> Option BQuick Check:
map = transform each item [OK]
- Confusing map with filter
- Thinking map sorts or counts items
- Assuming map removes items
seq in Kotlin?Solution
Step 1: Identify correct filter syntax
In Kotlin,filtertakes a lambda with condition inside curly braces:{ it % 2 == 0 }.Step 2: Check other options
mapchanges values, not filters; seq.filter(it % 2 == 0) misses braces; seq.map { it / 2 == 0 } uses wrong condition.Final Answer:
seq.filter { it % 2 == 0 } -> Option AQuick Check:
filter uses braces and condition [OK]
- Using map instead of filter
- Missing curly braces in lambda
- Wrong condition inside lambda
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)Solution
Step 1: Apply filter to keep even numbers
Filter keeps numbers whereit % 2 == 0, so 2 and 4 remain.Step 2: Apply map to multiply each by 10
Mapping multiplies each by 10: 2 * 10 = 20, 4 * 10 = 40.Final Answer:
[20, 40] -> Option CQuick Check:
filter evens then map *10 = [20, 40] [OK]
- Mapping before filtering
- Confusing filter condition
- Forgetting to convert sequence to list
val seq = sequenceOf(1, 2, 3, 4) val filtered = seq.filter(it % 2 == 0) println(filtered.toList())
Solution
Step 1: Check filter lambda syntax
Filter requires a lambda with curly braces:{ it % 2 == 0 }. The code misses braces.Step 2: Validate other options
Map is not used here; sequence can convert to list; condition can be any boolean expression.Final Answer:
Missing curly braces around lambda in filter -> Option AQuick Check:
filter needs braces for lambda [OK]
- Omitting braces in lambda
- Confusing filter and map
- Wrong filter condition assumed as error
seq = sequenceOf("apple", "banana", "cherry", "date"), which Kotlin code correctly filters strings containing letter 'a' and converts them to uppercase?Solution
Step 1: Filter strings containing 'a'
Usefilter { it.contains('a') }to keep only strings with 'a'. This keeps "apple", "banana", "date".Step 2: Convert filtered strings to uppercase
Usemap { it.uppercase() }to change these strings to uppercase.Final Answer:
seq.filter { it.contains('a') }.map { it.uppercase() }.toList() -> Option DQuick Check:
Filter then map uppercase = seq.filter { it.contains('a') }.map { it.uppercase() }.toList() [OK]
- Mapping before filtering changes all strings
- Using lowercase instead of uppercase
- Filtering after mapping changes string case before filter
Ready to prove it?
Pass the test and this pattern moves unknown
