Bird
Raised Fist0
Kotlinprogramming~20 mins

Generating infinite sequences in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Infinite Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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())
A[1, 4, 7, 10, 13]
B[2, 4, 6, 8, 10]
C[1, 2, 3, 4, 5]
D[1, 3, 5, 7, 9]
Attempts:
2 left
💡 Hint
Look at how the sequence starts and how the lambda adds 2 each time.
🧠 Conceptual
intermediate
1:30remaining
Understanding termination of infinite sequences
Which statement about Kotlin infinite sequences is true?
AInfinite sequences in Kotlin are stored fully in memory.
BAn infinite sequence always runs forever and cannot be limited.
CYou can limit an infinite sequence by using terminal operations like take or first.
DYou must convert an infinite sequence to a list before using it.
Attempts:
2 left
💡 Hint
Think about how you can get a finite result from an infinite source.
❓ Predict Output
advanced
2: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())
A[0, 1, 1, 2, 3, 5, 8]
B[1, 1, 2, 3, 5, 8, 13]
C[0, 1, 2, 3, 5, 8, 13]
D[1, 2, 3, 5, 8, 13, 21]
Attempts:
2 left
💡 Hint
The sequence generates pairs where the first is the current Fibonacci number.
❓ Predict Output
advanced
2: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())
A[1, 2, 3, 4, 5]
BThrows IllegalStateException due to null in sequence
C[1, 2, 3, 4]
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Check what happens when the lambda returns null.
📝 Syntax
expert
2: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.
Aval seq = generateSequence(1) { it + it }
Bval seq = generateSequence(1) { it * 2 }
Cval seq = generateSequence(1) { it xor 2 }
Dval seq = generateSequence(1) { it ** 2 }
Attempts:
2 left
💡 Hint
Check Kotlin operators for multiplication and exponentiation.

Practice

(1/5)
1. What does an infinite sequence in Kotlin do?
sequence { yield(1); yieldAll(generateSequence(2) { it + 1 }) }
easy
A. Generates only the first two numbers and stops
B. Creates a fixed list of numbers from 1 to 10
C. Generates values endlessly, one at a time, starting from 1
D. Throws an error because sequences can't be infinite

Solution

  1. Step 1: Understand sequence behavior

    The sequence starts by yielding 1, then continues with an infinite sequence starting at 2, increasing by 1 each time.
  2. Step 2: Recognize infinite generation

    Since generateSequence with a lambda that adds 1 never returns null, it produces values endlessly.
  3. Final Answer:

    Generates values endlessly, one at a time, starting from 1 -> Option C
  4. Quick Check:

    Infinite sequence = endless values [OK]
Hint: Infinite sequences produce values one by one without stopping [OK]
Common Mistakes:
  • Thinking infinite sequences create fixed lists
  • Assuming infinite sequences cause errors immediately
  • Believing sequences stop after a few values
2. Which of the following is the correct syntax to create an infinite sequence of even numbers starting from 0 in Kotlin?
easy
A. sequence { yield(0); yield(2); yield(4) }
B. sequenceOf(0, 2, 4, 6)
C. generateSequence(0, 2)
D. generateSequence(0) { it + 2 }

Solution

  1. Step 1: Identify infinite sequence syntax

    generateSequence(seed) { nextValue } creates an infinite sequence by applying the lambda repeatedly.
  2. Step 2: Check each option

    generateSequence(0) { it + 2 } correctly uses generateSequence(0) { it + 2 } to produce even numbers endlessly. Options B and D create finite sequences. generateSequence(0, 2) has incorrect syntax.
  3. Final Answer:

    generateSequence(0) { it + 2 } -> Option D
  4. Quick Check:

    Correct syntax = generateSequence(0) { it + 2 } [OK]
Hint: Use generateSequence(seed) { next } for infinite sequences [OK]
Common Mistakes:
  • Using sequenceOf for infinite sequences
  • Passing two arguments to generateSequence incorrectly
  • Not providing a lambda for next values
3. What will be the output of this Kotlin code?
val seq = generateSequence(1) { it + 3 }
println(seq.take(4).toList())
medium
A. [1, 2, 3, 4]
B. [1, 4, 7, 10]
C. [1, 3, 6, 9]
D. [4, 7, 10, 13]

Solution

  1. Step 1: Understand sequence generation

    The sequence starts at 1, then each next value adds 3: 1, 4, 7, 10, ...
  2. Step 2: Apply take and toList

    take(4) takes first 4 values: 1, 4, 7, 10. toList() converts them to a list.
  3. Final Answer:

    [1, 4, 7, 10] -> Option B
  4. Quick Check:

    Sequence values = [1,4,7,10] [OK]
Hint: Add the step value repeatedly starting from seed [OK]
Common Mistakes:
  • Adding 1 instead of 3 each step
  • Confusing take with drop
  • Expecting a finite sequence without take
4. Identify the error in this Kotlin code that tries to generate an infinite sequence of odd numbers:
val odds = generateSequence(1) { it + 2 }
println(odds.take(5).toList())
medium
A. No error; code runs correctly and prints odd numbers
B. Missing yield keyword inside generateSequence
C. The lambda should return null to stop sequence
D. Seed value should be 0 instead of 1

Solution

  1. Step 1: Check generateSequence usage

    The code uses generateSequence(1) { it + 2 }, which correctly generates odd numbers starting at 1.
  2. Step 2: Verify output and syntax

    take(5).toList() safely takes first 5 odd numbers. No syntax or runtime error exists.
  3. Final Answer:

    No error; code runs correctly and prints odd numbers -> Option A
  4. Quick Check:

    Correct infinite sequence = No error; code runs correctly and prints odd numbers [OK]
Hint: generateSequence needs only seed and next lambda, no yield [OK]
Common Mistakes:
  • Thinking yield is needed inside generateSequence
  • Expecting lambda to return null to continue
  • Confusing seed value for even numbers
5. You want to generate an infinite sequence of Fibonacci numbers in Kotlin. Which code correctly creates this infinite sequence?
hard
A. Both A and B
B. sequence { var a=0; var b=1; while(true) { yield(a); val c=a+b; a=b; b=c } }
C. generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first }
D. generateSequence(0) { it + it }

Solution

  1. Step 1: Analyze generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first }

    generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }.map { it.first } uses generateSequence with pairs to generate Fibonacci pairs, then maps to first element, correctly producing Fibonacci numbers infinitely.
  2. Step 2: Analyze sequence { var a=0; var b=1; while(true) { yield(a); val c=a+b; a=b; b=c } }

    sequence { var a=0; var b=1; while(true) { yield(a); val c=a+b; a=b; b=c } } uses a sequence builder with a while(true) loop yielding Fibonacci numbers step-by-step, also correct and infinite.
  3. Step 3: Analyze generateSequence(0) { it + it }

    generateSequence(0) { it + it } doubles the number each time, not Fibonacci, so it's incorrect.
  4. Final Answer:

    Both A and B -> Option A
  5. Quick Check:

    Both methods generate Fibonacci infinite sequences [OK]
Hint: Use pairs or sequence builder with yield for Fibonacci [OK]
Common Mistakes:
  • Using generateSequence without pairs for Fibonacci
  • Trying to double numbers instead of summing previous two
  • Not using yield in sequence builder for infinite values
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