Pergunta de entrevista da empresa Block

Regular CS and algorithm question. Nothing tricky.