Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Kotlin Puzzles
Programming

Kotlin Puzzles

Small Kotlin code puzzles that reveal surprising language behaviors and help sharpen your understanding of Kotlin.

Eugene Obiedkov

by Eugene Obiedkov

Full Stack Developer

Mar, 2026
8 min read

facebooklinkedintwitter
copy
Kotlin Puzzles

Kotlin is known for being concise, safe, and expressive. However, even experienced developers can sometimes be surprised by how certain pieces of Kotlin code behave. Small details such as type inference, null safety, ranges, lambda behavior, or collection operations may lead to results that are not immediately obvious.

In this article, we will explore several Kotlin puzzles. Each puzzle contains a short piece of code and a simple question:

What will this code print?

At first glance, the answer might seem obvious. But Kotlin has some subtle behaviors that can produce results different from what you might expect.

Try to guess the output before reading the explanation.

Puzzle 1: Mutable List or Not?

What will this code print?


fun main() {
    val numbers = listOf(1, 2, 3)
    val mutable = numbers

    if (mutable is MutableList) {
        mutable.add(4)
    }

    println(numbers)
}

    Answer [1, 2, 3]

    The function listOf() returns a read-only list, not a mutable one. Even though Kotlin collections are implemented on top of Java collections, the type returned by listOf() is treated as a read-only List. When the code checks whether mutable is a MutableList, the condition is false, so the block that adds the value 4 never executes. As a result, the original list is printed unchanged.

Puzzle 2: When Does the Lambda Execute?

What will this code print?


fun main() {
    val numbers = listOf(1, 2, 3)

    val result = numbers.map {
        println("Processing $it")
        it * 2
    }

    println("Result size: ${result.size}")
}

    Answer Processing 1 Processing 2 Processing 3 Result size: 3

    Many developers assume that operations like map() might behave lazily, but this is not the case for regular Kotlin collections. The map() function immediately iterates through the list and applies the lambda to every element. Each element triggers the println inside the lambda before the new list is created. After all elements are processed, the program prints the size of the resulting list, which contains three values.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

Puzzle 3: The Range and Step Puzzle

What will this code print?


fun main() {
    for (i in 1..10 step 3) {
        print(i)
    }
}

    Answer 14710

    The expression 1..10 creates an inclusive range that starts at 1 and ends at 10. The step function controls how the range moves forward during iteration. Starting at 1, the loop increases the value by 3 on every iteration. This produces the sequence 1, 4, 7, and finally 10. The loop stops after reaching 10 because it is the upper boundary of the range.

Puzzle 4: Safe Call with let

What will this code print?


fun main() {
    var text: String? = "Kotlin"

    text?.let {
        println(it.length)
    }

    text = null

    text?.let {
        println(it.length)
    }

    println("Done")
}

    Answer 6 Done

    The safe call operator ?. executes the following block only if the variable is not null. At the beginning of the program, text contains the string "Kotlin", so the let block runs and prints the length of the string, which is 6. After that, the variable is explicitly set to null. The second safe call checks the value again, but because the variable is now null, the let block is skipped entirely. The program then continues normally and prints "Done".

Puzzle 5: The Increment Operator Trap

What will this code print?


fun main() {
    var a = 5
    val b = a++ + ++a

    println(b)
}

    Answer 12

    The expression uses both postfix and prefix increment operators, which behave differently. The postfix operator a++ returns the current value and only then increases the variable. At that moment the value used in the expression is 5, and afterward a becomes 6. The prefix operator ++a increments the variable first and then returns the new value. Since a is already 6, it becomes 7 and the value 7 is used in the expression. The final result of the calculation is therefore 5 + 7, which equals 12.

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

Summary

In these puzzles, we explored several examples where Kotlin behaves differently than one might initially expect. Understanding these details helps developers read code more confidently, avoid subtle bugs, and write more reliable Kotlin programs.

Was dit artikel nuttig?

Delen:

facebooklinkedintwitter
copy

Was dit artikel nuttig?

Delen:

facebooklinkedintwitter
copy

Inhoud van dit artikel

Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt