Challenge - 5 Problems
Type-Safe Builder Master
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 type-safe builder code?
Consider this Kotlin code using a type-safe builder pattern. What will it print?
Kotlin
class Html { private val children = mutableListOf<String>() fun body(init: Body.() -> Unit) { val body = Body() body.init() children.add(body.render()) } fun render() = "<html>" + children.joinToString() + "</html>" } class Body { private val texts = mutableListOf<String>() fun p(text: String) { texts.add("<p>$text</p>") } fun render() = texts.joinToString("") } fun html(init: Html.() -> Unit): Html { val html = Html() html.init() return html } fun main() { val page = html { body { p("Hello") p("World") } } println(page.render()) }
Attempts:
2 left
💡 Hint
Look at how the Body class renders its paragraphs and how Html collects children.
✗ Incorrect
The Html class collects the rendered Body content as a string without adding tags explicitly. The Body class renders paragraphs as
elements concatenated. So the final output is
Hello
World
.🧠 Conceptual
intermediate1:30remaining
Which Kotlin feature enables type-safe builders to restrict DSL usage?
In Kotlin type-safe builders, which language feature helps restrict the scope of DSL functions to prevent misuse?
Attempts:
2 left
💡 Hint
This feature marks DSL receiver classes to avoid mixing scopes.
✗ Incorrect
The @DslMarker annotation is used to mark DSL receiver classes to restrict implicit receivers and prevent accidental calls to functions from the wrong scope.
🔧 Debug
advanced2:00remaining
Why does this Kotlin type-safe builder code cause a compilation error?
Examine the code below. Why does it fail to compile?
Kotlin
class Container { fun addItem(item: String) { println("Added: $item") } } fun container(init: Container.() -> Unit): Container { val c = Container() c.init() return c } fun main() { container { addItem("First") addItem(123) // Error here } }
Attempts:
2 left
💡 Hint
Check the parameter type of addItem and the argument passed.
✗ Incorrect
The addItem function expects a String argument, but 123 is an Int literal, causing a type mismatch compilation error.
📝 Syntax
advanced1:30remaining
Which option correctly defines a Kotlin type-safe builder function with receiver?
Select the correct syntax for a Kotlin function that takes a lambda with receiver to build a DSL.
Attempts:
2 left
💡 Hint
Look for the correct lambda with receiver syntax and return type.
✗ Incorrect
Option B correctly uses a lambda with receiver (Builder.() -> Unit), calls it with b.init(), and returns the Builder instance.
🚀 Application
expert1:30remaining
How many items are in the resulting list after this Kotlin type-safe builder runs?
Given this Kotlin code using a type-safe builder, how many items does the list contain after execution?
Kotlin
class ListBuilder { private val items = mutableListOf<String>() fun item(value: String) { items.add(value) } fun build() = items } fun list(init: ListBuilder.() -> Unit): List<String> { val builder = ListBuilder() builder.init() return builder.build() } fun main() { val result = list { item("A") item("B") item("C") } println(result.size) }
Attempts:
2 left
💡 Hint
Count how many times item() is called inside the builder.
✗ Incorrect
The builder adds three items: "A", "B", and "C". So the resulting list size is 3.