Partition for splitting in Kotlin - Time & Space Complexity
When we split a list into two parts based on a condition, we want to know how long it takes as the list grows.
How does the work increase when the list gets bigger?
Analyze the time complexity of the following code snippet.
val numbers = listOf(1, 2, 3, 4, 5, 6)
val (even, odd) = numbers.partition { it % 2 == 0 }
println("Even numbers: $even")
println("Odd numbers: $odd")
This code splits a list of numbers into two lists: one with even numbers and one with odd numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each number once to see if it is even or odd.
- How many times: Exactly once for every item in the list.
As the list gets bigger, the program checks each number one time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the size of the list.
Time Complexity: O(n)
This means the time to split the list grows in a straight line with the number of items.
[X] Wrong: "Partitioning takes more time because it creates two lists and does extra work."
[OK] Correct: The main work is just checking each item once; creating two lists is done while checking, so it does not add extra loops.
Understanding how partition works helps you explain how simple loops handle splitting data efficiently in real programs.
"What if we changed the condition to check each item twice inside the partition? How would the time complexity change?"
Practice
partition function do in Kotlin when applied to a list?Solution
Step 1: Understand the purpose of partition
Thepartitionfunction divides a list into two parts based on a condition.Step 2: Recognize the return type
It returns aPairwhere the first list contains elements matching the condition, and the second list contains the rest.Final Answer:
Splits the list into two lists based on a condition, returning a Pair of lists. -> Option DQuick Check:
partition = split by condition [OK]
- Thinking partition sorts the list
- Confusing partition with filtering
- Assuming partition removes duplicates
partition on a list numbers to split even and odd numbers?Solution
Step 1: Check the lambda syntax inside partition
The condition must be inside curly braces withitrepresenting each element.Step 2: Verify destructuring declaration
Partition returns a Pair, so useval (first, second) = ...to get both lists.Final Answer:
val (evens, odds) = numbers.partition { it % 2 == 0 } -> Option AQuick Check:
Correct lambda and destructuring = val (evens, odds) = numbers.partition { it % 2 == 0 } [OK]
- Missing curly braces for lambda
- Not using destructuring for Pair
- Using parentheses instead of braces for lambda
val words = listOf("apple", "banana", "avocado", "berry")
val (aWords, otherWords) = words.partition { it.startsWith('a') }
println(aWords)
println(otherWords)Solution
Step 1: Apply the condition to each word
Check which words start with 'a': "apple" and "avocado" match.Step 2: Partition splits into two lists
First list has words starting with 'a', second list has the rest: "banana", "berry".Final Answer:
["apple", "avocado"] ["banana", "berry"] -> Option AQuick Check:
partition by startsWith('a') = ["apple", "avocado"] ["banana", "berry"] [OK]
- Mixing order of lists in output
- Forgetting that partition returns Pair of lists
- Confusing startsWith with contains
partition:
val nums = listOf(1, 2, 3, 4)
val evens, odds = nums.partition { it % 2 == 0 }
println(evens)
println(odds)Solution
Step 1: Check destructuring declaration syntax
Destructuring a Pair requires parentheses:val (evens, odds) = ....Step 2: Confirm lambda and usage
The lambda is correct for evens; partition works on lists of integers; println is fine.Final Answer:
Missing parentheses around evens and odds for destructuring declaration. -> Option CQuick Check:
Destructuring needs parentheses [OK]
- Omitting parentheses in destructuring
- Changing correct lambda unnecessarily
- Thinking partition only works on strings
partition to separate positive numbers from zero and negatives?
Choose the correct code snippet.Solution
Step 1: Define the condition for positives
Positive numbers are greater than zero, so condition isit > 0.Step 2: Use partition with this condition
Partition splits list into positives and the rest (zero and negatives).Final Answer:
val (positives, nonPositives) = numbers.partition { it > 0 } -> Option BQuick Check:
Partition by it > 0 separates positives [OK]
- Using it >= 0 includes zero as positive
- Using it < 0 reverses groups
- Using it == 0 only selects zeros
Ready to prove it?
Pass the test and this pattern moves unknown
