I gave interview in Amazon for SDE 2 role. In the 2nd round I was this question. Interview was on video call:-
Goal : Mario needs to Rescue Peach inside a castle of Lava.
Facts :
Examples :
Peach at (3,3) , safe spots (1,1), (2,2) → 1 jump (0,0) - (3,3)
Peach at (5,5), safe spots (1,1), (2,2), (3,3), (4,4) → 2 jumps (0,0) - (3,3) - (5,5)
Peach at (10, 0) safe spots (0,4), (0, 5), (5,5), (10,5) → 4 jumps
Peach at (5,5) safe spots (0,1), (1,0), (1,1) → -1 (cannot rescue Peach)
public class Spot {
public int x; // x index
public int y; // y index
}
public interface Map{
public Spot getPeachSpot();
public List<Spot> getSafeSpots();
}
public int calculateMinimumJumps(final Map map) {
// code here
}I am not able to solve this correctly. I tried using DFS. Please suggest solution