Fewest Dice Rolls to the Last Square
Updated
Problem statement
A board game is played on an n x n grid board. The squares are numbered from 1 to n * n. Numbering starts at the bottom-left square (board[n - 1][0]) and goes left to right along the bottom row. The next row up goes right to left, the one after that left to right again, and so on, switching direction on every row.
If board[r][c] is not -1, the square at row r, column c has a jump: any player who lands there is moved to square board[r][c]. A jump can go forward (a ladder) or back (a snake).
Your token begins on square 1. In one move you roll a six-sided die and choose the result: from square x you go to any square from x + 1 to min(x + 6, n * n). If that square has a jump, you must take it. A move uses at most one jump: if the jump ends on another square with a jump, you stop there anyway.
Return the fewest moves needed to reach square n * n, or -1 if it cannot be reached.
Function
snakesAndLadders(board: int[][]) → intExamples
Example 1
board = [[-1,-1,-1,-1],[-1,-1,-1,-1],[-1,2,-1,-1],[-1,-1,15,-1]]return = 2The squares are numbered 1 to 16, starting at the bottom left and changing direction on each row. From square 1, a roll of 2 lands on square 3, whose jump takes you to square 15. A roll of 1 then reaches square 16. That is 2 moves. One move is not enough: the highest square you can land on from 1 is 7, and square 7 sends you down to 2.
Example 2
board = [[1,-1,-1],[1,1,1],[-1,1,1]]return = -1From square 1 you can land on squares 2 to 7, and every one of them sends you back to square 1. You can never get past square 1, so the answer is -1.
Example 3
board = [[-1,-1,-1,-1],[-1,16,-1,-1],[-1,-1,-1,-1],[-1,10,-1,-1]]return = 2A roll of 1 lands on square 2, which jumps you to square 10. The jump on square 10 is not used, because a move takes at most one jump. From square 10, a roll of 6 lands on square 16. That is 2 moves.
Constraints
n == board.length == board[i].length2 <= n <= 20board[i][j]is-1or a square number from1ton * n- Squares
1andn * nhave no jump (their value is-1).
More Rippling Onsite Interview questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delivery Cost TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Maximum Currency Conversion Along a Simple PathPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent WordsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Future-Effective Employee SalaryONSITE INTERVIEW · Seen Aug 2026
- Filter and Sort Scheduled TasksONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Time to Rot All OrangesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Rotting OrangesOtter.ai · ONSITE INTERVIEWOtter.ai interview questions
- Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Maximum Number of Fish in a GridGlobalization Partners · ONSITE INTERVIEW
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)
- Rippling SWE Interviews, No Level: 156 Experiences (Part 1 of 6)