Challenge - 5 Problems
Infinite Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple infinite sequence with take
What is the output of this Kotlin code snippet?
Kotlin
val seq = generateSequence(1) { it + 2 } println(seq.take(5).toList())
Attempts:
2 left
💡 Hint
Look at how the sequence starts and how the lambda adds 2 each time.
✗ Incorrect
The sequence starts at 1 and each next element adds 2, so the first five elements are 1, 3, 5, 7, 9.
🧠 Conceptual
intermediate1:30remaining
Understanding termination of infinite sequences
Which statement about Kotlin infinite sequences is true?
Attempts:
2 left
💡 Hint
Think about how you can get a finite result from an infinite source.
✗ Incorrect
Infinite sequences generate values lazily. Using functions like take(n) or first() stops the sequence after a finite number of elements.
❓ Predict Output
advanced2:30remaining
Output of a Fibonacci infinite sequence
What is the output of this Kotlin code?
Kotlin
val fibs = generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) } println(fibs.take(7).map { it.first }.toList())
Attempts:
2 left
💡 Hint
The sequence generates pairs where the first is the current Fibonacci number.
✗ Incorrect
The sequence starts with (0,1). Each next pair is (previous second, sum of previous first and second). Taking first elements gives Fibonacci numbers starting at 0.
❓ Predict Output
advanced2:00remaining
Terminating a sequence with null
What is the output of this Kotlin code?
Kotlin
val seq = generateSequence(1) { if (it < 5) it + 1 else null } println(seq.toList())
Attempts:
2 left
💡 Hint
Check what happens when the lambda returns null.
✗ Incorrect
The lambda returns null when it >= 5, which signals the end of the sequence. toList() collects [1, 2, 3, 4, 5] and stops. No error occurs.
📝 Syntax
expert2:30remaining
Which option produces a valid infinite sequence of powers of 2?
Select the Kotlin code snippet that correctly generates an infinite sequence of powers of 2 starting at 1.
Attempts:
2 left
💡 Hint
Check Kotlin operators for multiplication and exponentiation.
✗ Incorrect
Option B uses it * 2 which doubles the value each time, producing powers of 2. Option B also doubles but is equivalent to C. Option B uses ^ which is bitwise XOR in Kotlin, not exponentiation. Option B uses ** which is invalid syntax in Kotlin.