Pregunta de entrevista de Meta

Leetcode question. Difficulty was around a medium