Solve budget queries and shortest path | Microsoft Interview Question