Minimum Moves to Capture the King
Updated
Problem statement
A piece starts at start on an empty rows-by-cols board and must capture the king at king.
For each move, choose exactly one row-and-column offset from directions and add it to the piece’s current position. It may not leave the board. Return the minimum number of moves needed to reach the king, or -1 if the king is unreachable.
Function
minimumMovesToCaptureKing(rows: int, cols: int, start: int[], king: int[], directions: int[][]) → intExamples
Example 1
rows = 8cols = 8start = [0,0]king = [7,7]directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]return = 7Diagonal moves are allowed, so the piece can step down and to the right each time. Seven such moves take it from the top-left corner to the king in the opposite corner.
Example 2
rows = 8cols = 8start = [0,0]king = [7,7]directions = [[-1,0],[0,-1],[0,1],[1,0]]return = 14Only one-cell moves up, down, left or right are allowed, so no move is diagonal. The piece must walk down the whole board and across the whole board one step at a time, which takes fourteen moves.
Constraints
1 <= rows, cols <= 200.- Start and king are valid board cells.
1 <= directions.length <= 8.- Each direction is a row change and a column change. Both are whole numbers from
-1to1, and they are not both zero. No two directions are the same.
More Amazon Phone Screen questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Word Chain Jumps in a GridGoogle · PHONE SCREENGoogle interview questions
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Count Connected IslandsUber · PHONE SCREENUber interview questions
- Number of IslandsSpaceX · PHONE SCREENSpaceX interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Xiangqi Horse ReachabilityeBay · PHONE SCREENeBay interview questions
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions