Compute nested depth sum and grid distance | Meta Interview Question