Sliding Puzzle
Updated
Problem statement
The 2 x 3 board contains tiles 1 through 5 and one empty cell 0. In one move, swap the empty cell with a tile directly above, below, left or right of it.
Return the minimum moves needed to reach [[1,2,3],[4,5,0]]. Return -1 if the target is unreachable.
Function
slidingPuzzle(board: int[][]) → intExamples
Example 1
board = [[1,2,3],[4,0,5]]return = 1Swap the empty cell with tile 5.
Example 2
board = [[1,2,3],[5,4,0]]return = -1Swapping the two numbered tiles changes the component; no sequence reaches the target.
Example 3
board = [[4,1,2],[5,0,3]]return = 5The shortest path uses five legal swaps.
Constraints
boardhas exactly two rows and three columns.- Its six cells contain each integer from
0through5exactly once.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Maximum-Clearance Path From a PointGoogle · PHONE SCREENGoogle interview questions
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- 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