Fewest Dice Rolls to Finish a Snakes and Ladders Board
Last updated: November 29, 2026
Quick Overview
Given an n by n Snakes and Ladders board numbered boustrophedon style from the bottom-left, where some squares send you to another square, return the minimum number of six-sided die rolls to reach the last square, or -1.
0
0
0 solved
Given an n by n Snakes and Ladders board numbered boustrophedon style from the bottom-left, where some squares send you to another square, return the minimum number of six-sided die rolls to reach the last square, or -1.
An onsite question that layers a fiddly coordinate mapping on top of a BFS. Google interviewers use it to see whether you isolate the index conversion in a helper, test it, and then run a clean shortest-path search where each roll is one edge.
What the Interviewer Expects
- Write a helper mapping square number to row and column
- Handle the alternating direction of rows
- Model each roll as an edge to up to 6 squares
- Apply at most one snake or ladder per roll
- Use BFS for the minimum number of rolls
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if landing on a ladder top that is also a snake bottom chains moves?
- Compute the expected number of rolls with random play.
- What if the die has k faces?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
board[r][c]is -1 or the destination square of a snake or ladder.- Square 1 is bottom-left; rows alternate direction.
- From square s, a roll moves to s+1..s+6 (capped at n*n), then follows at mo...
High-Level Design
Squares are nodes; each roll gives up to 6 edges, so BFS finds the minimum rolls. The only hard part is translating square number s to a grid cell. With q, rem = divmod(s - 1, n), the row from the...