Two Sigma Interview Question

some bfs dfs question with math and binary search