Skip to content
tezvyn:

Explain and implement a higher-order function in Kotlin

Source: kotlinlang.orgMediumHow cards are made

Explain and implement a higher-order function in Kotlin

Tests your grasp of functions as first-class citizens. Define a higher-order function, explain it takes/returns functions, then implement filterAndTransform using the specified lambdas.

What's really being asked

This question tests your grasp of Kotlin's core functional programming features. It's not just about reciting a definition, but demonstrating you can work with functions as first-class citizens, understand function types like (Int) -> Boolean, and apply them to write clean, reusable code. The interviewer is checking if you can move beyond Java-style patterns and write idiomatic Kotlin.

The full answer

First, define a higher-order function as one that takes functions as parameters or returns a function. Second, explain this is possible because Kotlin treats functions as "first-class citizens," meaning they can be stored in variables, passed as arguments, and returned from other functions, just like an Int or a String. Third, write the filterAndTransform function. The signature is key: fun filterAndTransform(list: List<Int>, predicate: (Int) -> Boolean, transform: (Int) -> String): List<String>. Inside, iterate the list, apply the predicate, and if true, apply the transform, adding the result to a new list. Fourth, show how to call it, ideally using trailing lambda syntax for the last lambda parameter to demonstrate fluency.

The mistakes people make

A red flag is giving a vague definition like "it's a function that uses other functions" without mentioning passing them as parameters or returning them. Another major error is getting the function type syntax wrong, for example writing (Int, Boolean) instead of (Int) -> Boolean, which shows a fundamental misunderstanding. While the most idiomatic solution would be list.filter(predicate).map(transform), the prompt explicitly asks you to implement the higher-order function itself. Simply chaining standard library functions misses the point of the exercise.

What usually comes next

Expect follow-ups like: "How could you rewrite this using only standard library functions?" (Answer: list.filter(predicate).map(transform)). Or, "What is an inline function and why might you use it here?" (Answer: To reduce runtime overhead by avoiding the creation of function objects for lambdas; the compiler copies the function and lambda bodies to the call site). Another is, "What's the difference between a lambda and an anonymous function regarding the return keyword?"

A concrete example

fun filterAndTransform(

numbers: List<Int>,

predicate: (Int) -> Boolean, transform: (Int) -> String

): List<String> {
val result = mutableListOf<String>()
for (number in numbers) {
if (predicate(number)) {
result.add(transform(number))
}
}

return result }

// Example call:

val myNumbers = listOf(1, 2, 3, 4, 5, 6)
val evenStrings = filterAndTransform(myNumbers, { it % 2 == 0 }) { "Number $it" }

// evenStrings is now ["Number 2", "Number 4", "Number 6"]

Interview question

In Kotlin, what is the defining characteristic that makes a function a 'higher-order function'?

  • a.It must be defined using a lambda expression or an anonymous function.
  • b.It accepts a function as an argument or returns a function as its result.Correct
  • c.It calls at least one other function within its body to complete its task.
  • d.It operates on a collection of items, like a List or Map, to produce a new collection.
Why?

The correct answer is right because the definition of a higher-order function is one that treats functions as first-class citizens by taking them as parameters or returning them. While many higher-order functions operate on collections (a tempting distractor), this is a common use case, not the defining property.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles