Domanda di colloquio di Meta

2 algorithm question, the second one is harder.