tezvyn:

Explain higher-order functions and implement filterAndTransform

Curated by the Tezvyn teamSource: kotlinlang.orgintermediate
Explain higher-order functions and implement filterAndTransform

Tests whether you can define higher-order functions and use lambdas idiomatically in Kotlin. A strong answer defines HOFs as functions that take or return functions, then implements filterAndTransform with filter and map.

WHAT THIS TESTS: This question tests your understanding of Kotlin higher-order functions, function types, and lambda syntax in a practical coding context. It evaluates whether you know that functions are first-class values in Kotlin, meaning they can be stored in variables, passed as arguments, and returned from other functions. It also checks your familiarity with the standard library collection operators and whether you default to idiomatic functional patterns or imperative loops. Additionally, it reveals if you understand type inference for lambda expressions and the notation for function types such as (Int) -> Boolean and (Int) -> String.

A GOOD ANSWER COVERS: First, define a higher-order function as a function that either accepts another function as a parameter or returns a function, citing Kotlin function types explicitly. Second, implement filterAndTransform by applying the predicate to filter the input list and then applying the transform to map the remaining integers to strings, returning a List<String>. Third, explain that Kotlin supports type inference inside lambdas when the function type is declared in the parameter list, which allows concise syntax like { it > 0 }. Fourth, note that because functions are first-class, you can pass lambdas, anonymous functions, or function references interchangeably. Fifth, optionally mention that the filter and map operations create an intermediate collection, and that Sequence could be used for lazy evaluation on large inputs.

COMMON WRONG ANSWERS: First, using a mutable list and an explicit for-loop to build the result manually instead of chaining filter and map, which signals a lack of familiarity with idiomatic Kotlin. Second, confusing higher-order functions with generic functions: simply using type parameters like T or R does not make a function higher-order unless it also takes or returns a function. Third, reversing the order by mapping before filtering, which would pass strings to a predicate that expects an Int and therefore fail to compile. Fourth, omitting the definition of a higher-order function entirely and jumping straight to code without demonstrating conceptual understanding.

LIKELY FOLLOW-UPS: The interviewer may ask how to optimize this pattern to avoid lambda allocation overhead, which leads to inline functions and crossinline or noinline parameter modifiers. They might ask about the performance difference between eager filter and map on a List versus lazy evaluation using Sequence. They could also ask you to rewrite the lambdas as function references or to explain the difference between a lambda expression and an anonymous function.

ONE CONCRETE EXAMPLE: Consider the implementation: fun filterAndTransform(list: List<Int>, predicate: (Int) -> Boolean, transform: (Int) -> String): List<String> { return list.filter(predicate).map(transform) } If you call this with val numbers = listOf(1, 2, 3, 4, 5) and val result = filterAndTransform(numbers, { it > 2 }, { "Number: $it" }), the output is a list containing Number: 3, Number: 4, and Number: 5. This shows the predicate filtering the integers and the transform converting each surviving value to a formatted string.

Source: kotlinlang.org

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.