Pregunta de entrevista de Meta

Leetcode medium difficulty question with slight modification