Implement an Object-Oriented Snakes and Ladders Game
Updated
Problem statement
Implement a small Snakes and Ladders game using objects or structs that separate the board, player state and game-turn control. Use the deterministic game rules below.
The board has squares 1 through boardSize. All playerCount players start at 1 and have IDs 0 through playerCount-1. Player 0 moves first; subsequent rolls go to players in cyclic ID order, including after an overshoot. Consume the supplied die rolls in order.
On a turn, move ahead by the die value. If this would exceed the final square, stay put and apply no transport. Otherwise, if the landing square is a from entry in transports, follow that snake or ladder exactly once to its to square. Do not follow another transport from that destination in the same turn.
The first player ending a turn on the final square wins immediately. Ignore all later rolls. Return [winner, position0, position1, ...], where winner is the player ID or −1 if the supplied rolls end before anyone wins. Model the stated responsibilities with Board, Player and Game classes or equivalent structs; the automated checks assess the game’s returned behavior.
Function
playSnakesAndLadders(boardSize: int, playerCount: int, transports: int[][], rolls: int[]) → int[]Examples
Example 1
boardSize = 10playerCount = 2transports = [[3,8]]rolls = [2,1,2]return = [0,10,2]Player 0 lands on 3 and climbs to 8. Player 1 moves to 2. Player 0 then lands exactly on 10 and wins; positions are 10 and 2.
Example 2
boardSize = 10playerCount = 2transports = [[7,2]]rolls = [6]return = [-1,2,1]Player 0 lands on 7 and follows the snake to 2. Player 1 has not moved and no player has won.
Constraints
- For this exercise, assume
2 <= boardSize <= 1000and1 <= playerCount <= 6. 0 <= transports.length <= 200andtransports[i].length == 2. A pair is[from,to]with2 <= from < boardSize,1 <= to <= boardSizeandfrom != to. All from-squares are distinct.0 <= rolls.length <= 1000and1 <= rolls[i] <= 6.
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Sliding Items on a Circular RingIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)