Two Sigma interview question

some bfs dfs question with math and binary search