Pregunta de entrevista de Goldman Sachs

Asked two difficult maths questions and I need to write the program right away.