0
0
Kotlinprogramming~5 mins

RunBlocking for bridging in Kotlin - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: RunBlocking for bridging
O(1)
Understanding Time Complexity

We want to understand how the time needed to run code changes when using runBlocking in Kotlin.

Specifically, how does waiting for coroutines inside runBlocking affect the total time?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(5) { i ->
        launch {
            delay(100L)
            println("Task $i done")
        }
    }
}

This code runs 5 coroutines inside runBlocking, each delaying 100 milliseconds before printing.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Launching 5 coroutines inside a loop.
  • How many times: The loop runs 5 times, launching 5 separate tasks.
How Execution Grows With Input

Each coroutine waits for a fixed delay, but they run at the same time inside runBlocking.

Input Size (n)Approx. Operations
55 coroutines launched, total delay ~100ms
1010 coroutines launched, total delay ~100ms
100100 coroutines launched, total delay ~100ms

Pattern observation: The total time stays about the same because coroutines run concurrently, not one after another.

Final Time Complexity

Time Complexity: O(1)

The number of coroutines grows linearly with n (O(n)), but due to concurrency the total execution time remains constant O(1).

Common Mistake

[X] Wrong: "All coroutines inside runBlocking run one after another, so total time is delay times number of coroutines."

[OK] Correct: Coroutines launched inside runBlocking run concurrently, so delays overlap and total time is closer to the longest single delay, not the sum.

Interview Connect

Understanding how runBlocking manages coroutine execution helps you explain concurrency concepts clearly and shows you know how to bridge blocking and non-blocking code.

Self-Check

What if we replaced launch with async and awaited each result sequentially? How would the time complexity change?