Pregunta de entrevista de Morgan Stanley

puzzles, math, sql question with join, standard c/c++