Solve capital selection and grid escape | Two Sigma Interview Question