Pergunta de entrevista da empresa Glovo

Write a function that will sort items in a list where every next item will be greater or less then the previous, e.g. 5, 2, 1, 7, 9, 8 -> 2, 5, 1, 9, 7, 8

Resposta da entrevista

Sigiloso

18 de jun. de 2025

fun solution(list: List): List { for (i in 0 until list.size - 1) { val isEven = i % 2 == 0 if ((isEven && list[i] > list[i + 1]) || (!isEven && list[i] < list[i + 1])) { Collections.swap(list, i, i + 1) } } return list }