Pregunta de entrevista de Amazon

Typical algorithm complexity questions. Not difficult.