0
0
Kotlinprogramming~20 mins

GroupBy for categorization in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GroupBy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using groupBy?

Consider this Kotlin code that groups a list of words by their first letter. What will be printed?

Kotlin
val words = listOf("apple", "banana", "apricot", "blueberry", "cherry")
val grouped = words.groupBy { it.first() }
println(grouped)
A[[apple, apricot], [banana, blueberry], [cherry]]
B{apple=[a, p, p, l, e], banana=[b, a, n, a, n, a], apricot=[a, p, r, i, c, o, t], blueberry=[b, l, u, e, b, e, r, r, y], cherry=[c, h, e, r, r, y]}
C{a=[apple, apricot], b=[banana, blueberry], c=[cherry]}
D{a=[apple], b=[banana], a=[apricot], b=[blueberry], c=[cherry]}
Attempts:
2 left
💡 Hint

Remember that groupBy creates a map where keys are the grouping criteria and values are lists of items matching that key.

Predict Output
intermediate
2:00remaining
What is the output of grouping numbers by even or odd?

Given this Kotlin code, what will be printed?

Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6)
val grouped = numbers.groupBy { if (it % 2 == 0) "even" else "odd" }
println(grouped)
A{true=[2, 4, 6], false=[1, 3, 5]}
B[[1, 3, 5], [2, 4, 6]]
C{0=[2, 4, 6], 1=[1, 3, 5]}
D{even=[2, 4, 6], odd=[1, 3, 5]}
Attempts:
2 left
💡 Hint

Look at the string keys used in the groupBy lambda.

🔧 Debug
advanced
2:00remaining
Why does this Kotlin code cause a compilation error?

Look at this Kotlin code that tries to group a list of strings by length but causes a compilation error. What is the cause?

Kotlin
val words = listOf("cat", "dog", "bird")
val grouped = words.groupBy { length }
println(grouped)
AThe lambda uses 'length' without 'it.', so 'length' is undefined.
BThe list 'words' is immutable, so groupBy cannot be called.
CThe groupBy function requires a return type of Int, but 'length' is a String.
DThe println statement is missing parentheses.
Attempts:
2 left
💡 Hint

Check how to access the length property of each string inside the lambda.

🧠 Conceptual
advanced
2:00remaining
How many groups are created by this Kotlin groupBy call?

Given this code, how many keys will the resulting map have?

Kotlin
val items = listOf("red", "green", "blue", "yellow", "black")
val grouped = items.groupBy { it.length }
println(grouped.keys.size)
A5
B4
C3
D2
Attempts:
2 left
💡 Hint

Count the distinct lengths of the strings in the list.

Predict Output
expert
2:00remaining
What is the output of this Kotlin code grouping by first letter and counting?

What will this Kotlin code print?

Kotlin
val words = listOf("ant", "ape", "bat", "bear", "cat", "car")
val grouped = words.groupBy { it.first() }.mapValues { it.value.size }
println(grouped)
A{a=2, b=2, c=2}
B{a=2, b=3, c=2}
C{a=3, b=2, c=2}
D{a=[ant, ape], b=[bat, bear], c=[cat, car]}
Attempts:
2 left
💡 Hint

Look at how mapValues transforms the grouped map.