Bird
Raised Fist0
Kotlinprogramming~20 mins

FlatMap for nested collections in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FlatMap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of flatMap on nested lists
What is the output of this Kotlin code using flatMap on nested lists?
Kotlin
val nested = listOf(listOf(1, 2), listOf(3, 4))
val result = nested.flatMap { it.map { it * 2 } }
println(result)
A[1, 2, 3, 4]
B[4, 8]
C[[2, 4], [6, 8]]
D[2, 4, 6, 8]
Attempts:
2 left
💡 Hint
Remember that flatMap flattens one level after mapping.
❓ Predict Output
intermediate
1:30remaining
Result size after flatMap
Given this Kotlin code, how many elements does result contain?
Kotlin
val nested = listOf(listOf("a", "b"), listOf("c"))
val result = nested.flatMap { it }
println(result.size)
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
flatMap flattens one level of nested lists.
🔧 Debug
advanced
2:00remaining
Identify the error in flatMap usage
What error does this Kotlin code produce?
Kotlin
val nested = listOf(listOf(1, 2), listOf(3, 4))
val result = nested.flatMap { it * 2 }
println(result)
ANullPointerException at runtime
BNo error, output: [2, 4, 6, 8]
CType mismatch: 'it' is List<Int>, cannot multiply by Int
DIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check the operation inside the lambda for flatMap.
❓ Predict Output
advanced
2:00remaining
Output of flatMap with filtering
What is the output of this Kotlin code using flatMap with a filter?
Kotlin
val nested = listOf(listOf(1, 2, 3), listOf(4, 5))
val result = nested.flatMap { it.filter { it % 2 == 0 } }
println(result)
A[1, 3, 5]
B[2, 4]
C[2, 3, 4, 5]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Filter keeps only even numbers before flattening.
🧠 Conceptual
expert
2:30remaining
Why use flatMap instead of map for nested collections?
Which statement best explains why flatMap is preferred over map when working with nested collections in Kotlin?
AflatMap applies a transformation and flattens the result into a single list, avoiding nested lists.
BflatMap only works with lists of integers, while map works with any type.
CflatMap is faster because it uses parallel processing automatically.
DflatMap changes the original list, while map creates a new list.
Attempts:
2 left
💡 Hint
Think about the shape of the output collections.

Practice

(1/5)
1. What does the flatMap function do when applied to a nested list in Kotlin?
easy
A. It filters elements based on a condition.
B. It sorts the nested lists in ascending order.
C. It transforms each element and flattens the result into a single list.
D. It converts the list into a set to remove duplicates.

Solution

  1. Step 1: Understand flatMap behavior

    flatMap applies a transformation to each element and then flattens the nested results into one list.
  2. Step 2: Compare with other functions

    Unlike sorting, filtering, or converting to a set, flatMap combines mapping and flattening in one step.
  3. Final Answer:

    It transforms each element and flattens the result into a single list. -> Option C
  4. Quick Check:

    flatMap = transform + flatten [OK]
Hint: Remember: flatMap = map + flatten in one step [OK]
Common Mistakes:
  • Confusing flatMap with filter or sort
  • Thinking flatMap removes duplicates
  • Assuming flatMap only maps without flattening
2. Which of the following is the correct syntax to use flatMap on a list of lists named nestedList in Kotlin?
easy
A. nestedList.flatMap { it }
B. nestedList.flatMap()
C. nestedList.flatMap -> it
D. nestedList.flatMap { it.flatten() }

Solution

  1. Step 1: Identify correct lambda syntax

    The flatMap function requires a lambda to transform each element. { it } returns each inner list as is.
  2. Step 2: Check other options

    nestedList.flatMap() misses lambda, C uses wrong arrow syntax, D uses flatten() incorrectly on flat inner lists.
  3. Final Answer:

    nestedList.flatMap { it } -> Option A
  4. Quick Check:

    flatMap needs a lambda like { it } [OK]
Hint: flatMap always needs a lambda block { } [OK]
Common Mistakes:
  • Omitting the lambda block
  • Using arrow (->) instead of curly braces
  • Using flatten() instead of identity mapping
3. What is the output of the following Kotlin code?
val nested = listOf(listOf(1, 2), listOf(3, 4))
val result = nested.flatMap { it.map { it * 2 } }
println(result)
medium
A. [[2, 4], [6, 8]]
B. [1, 2, 3, 4]
C. [2, 4, [6, 8]]
D. [2, 4, 6, 8]

Solution

  1. Step 1: Understand inner map transformation

    Each inner list element is multiplied by 2, so listOf(1,2) becomes listOf(2,4), and listOf(3,4) becomes listOf(6,8).
  2. Step 2: Apply flatMap flattening

    flatMap flattens the transformed lists into one list: [2, 4, 6, 8].
  3. Final Answer:

    [2, 4, 6, 8] -> Option D
  4. Quick Check:

    flatMap + map doubles and flattens [OK]
Hint: flatMap flattens after map transformation [OK]
Common Mistakes:
  • Expecting nested lists instead of flat list
  • Ignoring the inner map multiplication
  • Confusing output with original nested list
4. Identify the error in this Kotlin code snippet using flatMap:
val nested = listOf(listOf(1, 2), listOf(3, 4))
val result = nested.flatMap { it * 2 }
println(result)
medium
A. flatMap requires a filter inside the lambda.
B. Cannot multiply a list by an integer directly.
C. The variable 'nested' is not defined.
D. flatMap should be replaced with map.

Solution

  1. Step 1: Analyze the lambda expression

    The lambda tries to multiply it (a list) by 2, which is invalid in Kotlin.
  2. Step 2: Understand flatMap requirements

    flatMap expects a lambda returning an iterable, but it * 2 causes a type error.
  3. Final Answer:

    Cannot multiply a list by an integer directly. -> Option B
  4. Quick Check:

    List * Int is invalid in Kotlin [OK]
Hint: Check types inside lambda; lists can't be multiplied by numbers [OK]
Common Mistakes:
  • Assuming list * number works like in some other languages
  • Thinking flatMap needs a filter
  • Confusing map and flatMap usage
5. Given a list of students where each student has a list of grades, how can you use flatMap to get a single list of all grades above 70?
data class Student(val name: String, val grades: List<Int>)
val students = listOf(
    Student("Alice", listOf(80, 65)),
    Student("Bob", listOf(90, 72)),
    Student("Carol", listOf(60, 85))
)
// Fill in the blank:
val passingGrades = students.flatMap { ________ }
println(passingGrades)
hard
A. it.grades.filter { grade -> grade > 70 }
B. it.grades.map { grade -> grade > 70 }
C. it.grades.filter { grade -> grade < 70 }
D. it.grades

Solution

  1. Step 1: Filter grades above 70 for each student

    Inside the lambda, use it.grades.filter { grade -> grade > 70 } to keep only passing grades.
  2. Step 2: Use flatMap to flatten all filtered grades

    flatMap combines all filtered lists into one flat list of passing grades.
  3. Final Answer:

    it.grades.filter { grade -> grade > 70 } -> Option A
  4. Quick Check:

    flatMap + filter extracts and flattens passing grades [OK]
Hint: Use flatMap with filter inside to flatten filtered results [OK]
Common Mistakes:
  • Using map instead of filter, resulting in booleans
  • Filtering for grades below 70 by mistake
  • Not filtering and returning all grades
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard