Domanda di colloquio di Meta

Two medium-difficulty algorithms, the interviewer was very kind.