Challenge - 5 Problems
Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of filter vs filterNot on a list
What is the output of the following Kotlin code?
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val filtered = numbers.filter { it % 2 == 0 } val filteredNot = numbers.filterNot { it % 2 == 0 } println("filtered: $filtered") println("filteredNot: $filteredNot")
Attempts:
2 left
💡 Hint
filter keeps elements matching the condition; filterNot keeps those that do not match.
✗ Incorrect
filter returns elements where the condition is true (even numbers), filterNot returns elements where the condition is false (odd numbers).
❓ Predict Output
intermediate1:30remaining
Result size after filter and filterNot
Given the code below, how many elements are in the list 'result' after filtering?
Kotlin
val words = listOf("apple", "banana", "cherry", "date") val result = words.filterNot { it.length <= 5 } println(result.size)
Attempts:
2 left
💡 Hint
filterNot keeps elements where the condition is false.
✗ Incorrect
Words with length <= 5 are filtered out; 'banana' and 'cherry' remain, so size is 2.
🔧 Debug
advanced2:00remaining
Why does this filterNot code cause a compilation error?
Identify the error in this Kotlin code snippet and select the correct explanation.
Kotlin
val nums = listOf(1, 2, 3, 4) val result = nums.filterNot { it > } println(result)
Attempts:
2 left
💡 Hint
Check the lambda expression syntax inside filterNot.
✗ Incorrect
The lambda 'it >' is incomplete and causes a syntax error because it lacks a right-hand operand.
🧠 Conceptual
advanced1:30remaining
Difference between filter and filterNot
Which statement correctly describes the difference between filter and filterNot in Kotlin?
Attempts:
2 left
💡 Hint
Think about what happens when the predicate is true or false.
✗ Incorrect
filter keeps elements where the predicate is true; filterNot keeps elements where the predicate is false.
🚀 Application
expert2:30remaining
Filter and filterNot combined usage
What is the output of this Kotlin code snippet?
Kotlin
val data = listOf("cat", "dog", "bird", "fish", "cow") val filtered = data.filter { it.length > 3 }.filterNot { it.contains('f') } println(filtered)
Attempts:
2 left
💡 Hint
First filter keeps words longer than 3 letters, then filterNot removes words containing 'f'.
✗ Incorrect
Words longer than 3 letters are "bird", "fish"; filterNot removes those containing 'f' so "fish" is removed, leaving only "bird".