Snakes and Ladders

Medium · Queue & Deque · about 25 minutes

Asked at Amazon, Google, Microsoft, ByteDance, Cisco, IBM, Palo Alto Networks, Zscaler.

An n × n board is numbered 1..n² in a boustrophedon pattern starting at the bottom-left: the bottom row goes left to right, the next row right to left, alternating. board[r][c] is -1, or the destination of a snake or ladder at that square. From square curr, roll a die to move to curr + 1 … min(curr + 6, n²); if that square has a snake or ladder, you must take it (but only once per move). Return the fewest moves to reach n², or -1.

Related guides