Morgan Stanley interview question

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