Interview frequency: …
Short answer
It is a function that takes another function as a parameter or returns a function as a result.
Explanation
Kotlin treats functions as first-class citizens, allowing functions to be stored in variables, passed, and returned using function types such as (T) -> R. When the lambda is the last parameter of the function, it can be written outside the parentheses of the call (trailing lambda syntax).
Code example
fun filterTransactions(
transactions: List<Double>,
predicate: (Double) -> Boolean
): List<Double> {
val result = mutableListOf<Double>()
for (t in transactions) {
if (predicate(t)) result.add(t)
}
return result
}
// Trailing lambda
val highValue = filterTransactions(transactions) { it > 1000.0 }Common mistakes
- Ignoring the cost of creating function objects in memory when the lambda call is repeated in a loop without using inline.