Pregunta de entrevista de Datadog

Question was algorithms-esque, pretty much LC medium