Map transformation in Kotlin - Time & Space Complexity
When we transform a map, we want to know how the time it takes changes as the map gets bigger.
We ask: How does the work grow when the map has more entries?
Analyze the time complexity of the following code snippet.
val originalMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val transformedMap = originalMap.mapValues { (key, value) -> value * 2 }
println(transformedMap)
This code takes each entry in the map and doubles its value, creating a new map.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The mapValues function visits each key-value pair once.
- How many times: Exactly once for each entry in the map.
As the map gets bigger, the work grows in a straight line with the number of entries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 visits to entries |
| 100 | 100 visits to entries |
| 1000 | 1000 visits to entries |
Pattern observation: Doubling the number of entries doubles the work.
Time Complexity: O(n)
This means the time to transform the map grows directly with the number of entries.
[X] Wrong: "Transforming a map is instant no matter the size."
[OK] Correct: Each entry must be visited once, so bigger maps take more time.
Understanding how map transformations scale helps you write efficient code and explain your choices clearly.
What if we used mapKeys instead of mapValues? How would the time complexity change?
Practice
map function do when applied to a list in Kotlin?Solution
Step 1: Understand the purpose of
Themapmapfunction applies a given transformation to each element of the list.Step 2: Check the effect on the list
It returns a new list with transformed elements without changing the original list.Final Answer:
It creates a new list by applying a transformation to each element. -> Option BQuick Check:
map transforms each item and returns new list [OK]
- Thinking map modifies the original list
- Confusing map with filter or sort
- Assuming map removes elements
map in Kotlin?Solution
Step 1: Identify correct lambda syntax
Themapfunction requires a lambda inside curly braces:{ it * 2 }.Step 2: Check the transformation logic
Doubling means multiplying by 2, soit * 2is correct.Final Answer:
val result = list.map { it * 2 } -> Option AQuick Check:
Correct lambda syntax with doubling [OK]
- Missing curly braces around lambda
- Using parentheses instead of braces
- Wrong operation inside lambda
val numbers = listOf(1, 2, 3)
val result = numbers.map { it + 5 }
println(result)
Solution
Step 1: Apply the map transformation
Each element innumbersis increased by 5: 1+5=6, 2+5=7, 3+5=8.Step 2: Form the new list
The new list is[6, 7, 8], which is printed.Final Answer:
[6, 7, 8] -> Option AQuick Check:
Each number +5 = [6, 7, 8] [OK]
- Confusing original list with transformed list
- Adding 5 only once instead of to each element
- Misreading the output format
val list = listOf(1, 2, 3)
val newList = list.map(it * 3)
println(newList)
Solution
Step 1: Check lambda syntax in map
Themapfunction requires a lambda inside curly braces, but here parentheses are used without braces.Step 2: Confirm correct usage
Correct syntax islist.map { it * 3 }to apply multiplication.Final Answer:
Missing curly braces around the lambda expression. -> Option DQuick Check:
Lambda needs braces in map [OK]
- Using parentheses instead of braces for lambda
- Thinking map needs mutable list
- Misunderstanding operator usage
val words = listOf("apple", "banana", "cherry"), how can you create a map where each word is a key and its length is the value using Kotlin's map transformation?Solution
Step 1: Understand the goal
We want a map where keys are words and values are their lengths.Step 2: Use map to create pairs and convert to map
words.map { it to it.length }creates pairs (word, length). ThentoMap()converts the list of pairs to a map.Final Answer:
val wordLengths = words.map { it to it.length } .toMap() -> Option CQuick Check:
Map pairs then convert to map [OK]
- Using associateWith is correct but not the asked map transformation
- Mapping only lengths returns list, not map
- Swapping key and value in pairs
Ready to prove it?
Pass the test and this pattern moves unknown
