Challenge - 5 Problems
Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Kotlin partition example?
Consider the following Kotlin code that uses the
partition function to split a list of integers into even and odd numbers. What will be printed?Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6) val (evens, odds) = numbers.partition { it % 2 == 0 } println("Evens: $evens") println("Odds: $odds")
Attempts:
2 left
💡 Hint
Remember, partition splits the list into two lists: one where the condition is true, and one where it is false.
✗ Incorrect
The
partition function returns a pair of lists. The first list contains elements where the predicate is true (even numbers), and the second list contains elements where the predicate is false (odd numbers).❓ Predict Output
intermediateWhat is the size of the lists after partition?
Given this Kotlin code, how many elements are in the first and second lists after partitioning by the condition
it > 3?Kotlin
val data = listOf(2, 3, 4, 5, 6) val (greaterThanThree, notGreaterThanThree) = data.partition { it > 3 } println("greaterThanThree size: ${greaterThanThree.size}") println("notGreaterThanThree size: ${notGreaterThanThree.size}")
Attempts:
2 left
💡 Hint
Count how many numbers in the list are greater than 3.
✗ Incorrect
Numbers greater than 3 are 4, 5, and 6 (3 elements). The rest (2 and 3) are not greater than 3 (2 elements).
❓ Predict Output
advancedWhat is the output when partitioning a list of strings by length?
Look at this Kotlin code that partitions a list of strings by whether their length is greater than 3. What will be printed?
Kotlin
val words = listOf("cat", "house", "dog", "elephant") val (longWords, shortWords) = words.partition { it.length > 3 } println("Long words: $longWords") println("Short words: $shortWords")
Attempts:
2 left
💡 Hint
Check the length of each word carefully.
✗ Incorrect
Words with length greater than 3 are "house" (5) and "elephant" (8). The others have length 3 or less.
❓ Predict Output
advancedWhat error occurs when using partition incorrectly?
What error will this Kotlin code produce?
Kotlin
val numbers = listOf(1, 2, 3) val (evens, odds) = numbers.partition { it % 2 == 0 } println(evens[3])
Attempts:
2 left
💡 Hint
Check the size of the evens list and the index accessed.
✗ Incorrect
The evens list contains [2]. Accessing index 3 is out of bounds, causing IndexOutOfBoundsException.
🧠 Conceptual
expertWhich option correctly describes the behavior of Kotlin's partition function?
Choose the statement that best describes what Kotlin's
partition function does when called on a collection.Attempts:
2 left
💡 Hint
Think about how partition divides elements based on a condition.
✗ Incorrect
Partition returns a pair of lists: one with elements where the predicate is true, and one where it is false.
