Challenge - 5 Problems
Timeout Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code using withTimeout?
Consider the following Kotlin code snippet using
withTimeout. What will be printed when this code runs?Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { try { withTimeout(1000L) { repeat(5) { i -> println("Processing $i") delay(400L) } } } catch (e: TimeoutCancellationException) { println("Timeout occurred") } }
Attempts:
2 left
💡 Hint
Think about how long each delay takes and the total timeout limit.
✗ Incorrect
The code tries to process 5 items with 400ms delay each, totaling 2000ms. The timeout is 1000ms, so it cancels during the third iteration after printing 'Processing 2'. Then it catches the timeout exception and prints 'Timeout occurred'.
❓ Predict Output
intermediate2:00remaining
What happens if delay exceeds withTimeout duration?
What will be the output of this Kotlin code snippet?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { try { withTimeout(500L) { delay(1000L) println("Finished") } } catch (e: TimeoutCancellationException) { println("Timeout!") } }
Attempts:
2 left
💡 Hint
The delay is longer than the timeout duration.
✗ Incorrect
The delay of 1000ms exceeds the 500ms timeout, so the coroutine is cancelled and the catch block prints 'Timeout!'.
🔧 Debug
advanced2:00remaining
Identify the error in this withTimeout usage
This Kotlin code snippet is intended to timeout after 1 second, but it does not behave as expected. What is the error?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { withTimeout(1000L) { repeat(3) { i -> println("Step $i") Thread.sleep(500L) } } println("Done") }
Attempts:
2 left
💡 Hint
Consider how blocking calls affect coroutine cancellation.
✗ Incorrect
Thread.sleep blocks the thread and does not cooperate with coroutine cancellation, so withTimeout cannot interrupt it. Using delay is required for proper timeout behavior.
📝 Syntax
advanced2:00remaining
Which option correctly uses withTimeout to return a value?
Select the Kotlin code snippet that correctly uses withTimeout to return a value from a suspending block.
Attempts:
2 left
💡 Hint
Remember how to return values from lambda blocks in Kotlin.
✗ Incorrect
Option C correctly returns the string "Success" from the withTimeout block and assigns it to result. Option C uses 'return' inside lambda which is invalid here. Option C returns Unit because println returns Unit. Option C has unreachable code after return.
🚀 Application
expert3:00remaining
How to handle multiple withTimeout calls with different durations?
You want to run two suspending tasks sequentially with different timeouts: first task with 1 second timeout, second with 2 seconds timeout. Which Kotlin code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Consider how to handle tasks that might timeout without throwing exceptions.
✗ Incorrect
Option D uses withTimeoutOrNull to safely handle timeouts by returning null instead of throwing exceptions. It assigns "Timeout" if the task exceeds its timeout. This is the best way to run multiple tasks with different timeouts sequentially and handle possible cancellations gracefully.