Bird
Raised Fist0
Kotlinprogramming~20 mins

Sequence operators (map, filter) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sequence Mastery
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 map and filter?
Consider this Kotlin code that uses sequence operators map and filter. What will it print?
Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5)
val result = numbers.filter { it % 2 == 1 }.map { it * 3 }.toList()
println(result)
A[6, 12, 18]
B[2, 4]
C[3, 9, 15]
D[1, 3, 5]
Attempts:
2 left
💡 Hint
First filter odd numbers, then multiply each by 3.
❓ Predict Output
intermediate
2:00remaining
What does this Kotlin code print after filtering and mapping?
Look at this Kotlin code using sequence operators. What is the output?
Kotlin
val words = sequenceOf("apple", "banana", "cherry", "date")
val result = words.filter { it.length > 5 }.map { it.uppercase() }.toList()
println(result)
A["BANANA", "CHERRY"]
B["apple", "banana", "cherry", "date"]
C["APPLE", "BANANA", "CHERRY", "DATE"]
D["date"]
Attempts:
2 left
💡 Hint
Filter words longer than 5 letters, then convert to uppercase.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code throw an exception?
This Kotlin code throws an exception. What is the cause?
Kotlin
val nums = sequenceOf(1, 2, 3, 4)
val result = nums.map { if (it == 3) null else it * 2 }.filter { it!! > 2 }.toList()
println(result)
AThrows ClassCastException due to null in sequence
BThrows IllegalArgumentException because map returns null
CNo exception, prints [4, 8]
DThrows NullPointerException because filter uses 'it!!' on null values
Attempts:
2 left
💡 Hint
Check how null values are handled in filter with 'it!!'.
📝 Syntax
advanced
2:00remaining
Which option is the correct Kotlin syntax to filter even numbers and double them using sequences?
Choose the correct Kotlin code that filters even numbers from a sequence and doubles them.
Aval result = sequenceOf(1,2,3,4).filter { it % 2 == 0 }.map { it * 2 }.toList()
Bval result = sequenceOf(1,2,3,4).map { it * 2 }.filter { it % 2 == 0 }.toList()
Cval result = sequenceOf(1,2,3,4).filter(it % 2 == 0).map(it * 2).toList()
Dval result = sequenceOf(1,2,3,4).filter { it % 2 == 0 }.map(it * 2).toList()
Attempts:
2 left
💡 Hint
Remember to use lambda syntax with curly braces and 'it'.
🚀 Application
expert
2:00remaining
How many items are in the resulting list after this Kotlin sequence operation?
Given this Kotlin code, how many items does the resulting list contain?
Kotlin
val data = sequenceOf(10, 15, 20, 25, 30)
val result = data.filter { it > 15 }.map { it / 5 }.filter { it % 2 == 0 }.toList()
println(result.size)
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Step through filtering and mapping carefully.

Practice

(1/5)
1. What does the map operator do in a Kotlin sequence?
easy
A. It removes items that do not match a condition.
B. It transforms each item in the sequence to a new value.
C. It sorts the items in the sequence.
D. It counts the number of items in the sequence.

Solution

  1. Step 1: Understand the purpose of map

    The map operator applies a function to each item, changing it to a new value.
  2. Step 2: Compare with other operators

    filter removes items, sort orders items, and count counts items, so these are different.
  3. Final Answer:

    It transforms each item in the sequence to a new value. -> Option B
  4. Quick Check:

    map = transform each item [OK]
Hint: Map changes items; filter removes items [OK]
Common Mistakes:
  • Confusing map with filter
  • Thinking map sorts or counts items
  • Assuming map removes items
2. Which of the following is the correct syntax to filter even numbers from a sequence seq in Kotlin?
easy
A. seq.filter { it % 2 == 0 }
B. seq.map { it % 2 == 0 }
C. seq.filter(it % 2 == 0)
D. seq.map { it / 2 == 0 }

Solution

  1. Step 1: Identify correct filter syntax

    In Kotlin, filter takes a lambda with condition inside curly braces: { it % 2 == 0 }.
  2. Step 2: Check other options

    map changes values, not filters; seq.filter(it % 2 == 0) misses braces; seq.map { it / 2 == 0 } uses wrong condition.
  3. Final Answer:

    seq.filter { it % 2 == 0 } -> Option A
  4. Quick Check:

    filter uses braces and condition [OK]
Hint: filter uses braces with condition inside [OK]
Common Mistakes:
  • Using map instead of filter
  • Missing curly braces in lambda
  • Wrong condition inside lambda
3. What is the output of this Kotlin code?
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)
medium
A. [10, 20]
B. [2, 4]
C. [20, 40]
D. [1, 3]

Solution

  1. Step 1: Apply filter to keep even numbers

    Filter keeps numbers where it % 2 == 0, so 2 and 4 remain.
  2. Step 2: Apply map to multiply each by 10

    Mapping multiplies each by 10: 2 * 10 = 20, 4 * 10 = 40.
  3. Final Answer:

    [20, 40] -> Option C
  4. Quick Check:

    filter evens then map *10 = [20, 40] [OK]
Hint: Filter first, then map transforms filtered items [OK]
Common Mistakes:
  • Mapping before filtering
  • Confusing filter condition
  • Forgetting to convert sequence to list
4. Find the error in this Kotlin code snippet:
val seq = sequenceOf(1, 2, 3, 4)
val filtered = seq.filter(it % 2 == 0)
println(filtered.toList())
medium
A. Missing curly braces around lambda in filter
B. Using map instead of filter
C. Sequence cannot be converted to list
D. Filter condition should be it % 2 != 0

Solution

  1. Step 1: Check filter lambda syntax

    Filter requires a lambda with curly braces: { it % 2 == 0 }. The code misses braces.
  2. Step 2: Validate other options

    Map is not used here; sequence can convert to list; condition can be any boolean expression.
  3. Final Answer:

    Missing curly braces around lambda in filter -> Option A
  4. Quick Check:

    filter needs braces for lambda [OK]
Hint: Always use braces {} for lambdas in filter [OK]
Common Mistakes:
  • Omitting braces in lambda
  • Confusing filter and map
  • Wrong filter condition assumed as error
5. Given a sequence of strings seq = sequenceOf("apple", "banana", "cherry", "date"), which Kotlin code correctly filters strings containing letter 'a' and converts them to uppercase?
hard
A. seq.filter { it.contains('a') }.map { it.lowercase() }.toList()
B. seq.map { it.uppercase() }.filter { it.contains('a') }.toList()
C. seq.map { it.lowercase() }.filter { it.contains('a') }.toList()
D. seq.filter { it.contains('a') }.map { it.uppercase() }.toList()

Solution

  1. Step 1: Filter strings containing 'a'

    Use filter { it.contains('a') } to keep only strings with 'a'. This keeps "apple", "banana", "date".
  2. Step 2: Convert filtered strings to uppercase

    Use map { it.uppercase() } to change these strings to uppercase.
  3. Final Answer:

    seq.filter { it.contains('a') }.map { it.uppercase() }.toList() -> Option D
  4. Quick Check:

    Filter then map uppercase = seq.filter { it.contains('a') }.map { it.uppercase() }.toList() [OK]
Hint: Filter first, then map to uppercase [OK]
Common Mistakes:
  • Mapping before filtering changes all strings
  • Using lowercase instead of uppercase
  • Filtering after mapping changes string case before filter
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