Pergunta de entrevista da empresa Distributed (UK)

I was asked to solve Algorithm Question