What does this print?
tailrec fun rev(
s: String,
acc: String = ""
): String {
return if (s.isEmpty()) acc
else rev(s.drop(1), s[0] + acc)
}
fun main() {
println(rev("code"))
}
[spoiler title="Solution"]
Answer:
edoc
Explanation:
tailrec optimizes tail-recursive calls into loops, preventing stack overflow. Here, each character is prepended to the accumulator while dropping from the original string.
[/spoiler]