Flatten.
input { 'a' : 1, 'b' : 2, 'c' : { 'f' : 10, 'g' : { 'm' : 12, 'n' : 15 } } }
expected output { 'a' : 1, 'b' : 2, 'c.f' : 10, 'c.g.m' : 12, 'c.g.n' : 15 }
I am not very well-versed in Kotlin.
The code-challenge website did not provide me an option to switch to Java, although Java is my first language of choice for code-challenge problems. Anything Android specific, of course, Kotlin is my first choice.
I was unable to solve the question using Kotlin's functional-style, declarative-programming paradigm, operator-chain mechanism.
Instead, interviewer suggested I can solve it in Imperative-programming paradigm, so I presented a Java solution in Kotlin syntax, after some struggling, and probably terrible "coding-speed".
fun main () {
val input = mapOf(
'a' to 1, 'b' to 2,
'c' to mapOf(
'f' to 10,
'g' to mapOf<Char, Any>(
'm' to 12,
'n' to 15)))
val output = mutableMapOf<String, Int>()
helper ( output, input, null )
println ( output )
}
fun helper ( output: MutableMap<String, Int>, input: Map<Char, Any>, lastKey: String? ) {
input.forEach { entry ->
run {
val currKey = if (lastKey != null) "${lastKey}.${entry.key}" else "${entry.key}"
when (entry.value) {
is Int -> {
output.put("'${currKey}'", entry.value as Int)
}
else -> {
helper(output, entry.value as Map<Char, Any>, currKey)
}
}
}
}
}
Received Rejection email today.
The only reason I can think of is that I had informed the interviewer that I had not used Kotlin Professionally as yet.