Check Knight Tour Configuration - Problem
Knight's Chess Adventure: Validating the Perfect Tour

Imagine a chess knight embarking on an epic journey across an n × n chessboard, with one ambitious goal: visit every single square exactly once, starting from the top-left corner (0,0).

You're given a special grid that supposedly records this knight's journey. Each cell grid[row][col] contains a number indicating the order in which the knight visited that position (0-indexed). For example, if grid[2][3] = 5, it means the knight visited position (2,3) as its 6th move.

Your Mission: Determine if this grid represents a valid knight's tour - meaning the knight could actually make this journey following proper chess rules.

Knight Movement Rules:
• Move in an 'L' shape: 2 squares in one direction + 1 square perpendicular
• This creates exactly 8 possible moves from any position
• Must visit all n² - 1 squares exactly once

Return true if the tour is valid, false otherwise.

Input & Output

example_1.py — Valid 3×3 Knight Tour
$ Input: grid = [[0,11,16,5,20,17,2,21,6],[23,4,15,10,1,18,7,22,3],[14,9,24,19,8,13,12,25,0]]
Output: true
💡 Note: This represents a valid knight's tour starting from (0,0) with move 0, and each consecutive move follows valid knight movement rules (L-shaped: 2+1 or 1+2 squares).
example_2.py — Invalid Knight Move
$ Input: grid = [[0,1,2],[5,4,3],[6,7,8]]
Output: false
💡 Note: Move 0→1 goes from (0,0) to (0,1), which is only 1 square horizontally. Knights must move in L-shape (2+1), so this is invalid.
example_3.py — Wrong Starting Position
$ Input: grid = [[2,0,1],[3,4,5],[6,7,8]]
Output: false
💡 Note: The knight must start at position (0,0), but grid[0][0] = 2, not 0. Invalid starting position.

Visualization

Tap to expand
🏰 Knight's Royal Tour ValidationThe Kingdom (Chess Board)012543🏰 StartRoyal Movement Rules♞ Knight✓ Valid L-shaped moves🚫 Invalid Tour Detected!Move 0→1: (0,0)→(0,1) is not an L-shaped knight move
Understanding the Visualization
1
📍 Map the Kingdom
First, we create a royal registry mapping each delivery number to its house address (position)
2
🏃‍♂️ Trace the Route
Follow the messenger's path by checking if each consecutive delivery follows valid knight movement rules
3
✅ Validate Each Hop
Ensure every move is L-shaped: exactly 2 squares in one direction plus 1 square perpendicular
4
🏁 Deliver Verdict
If any move breaks the knight's movement rules, the tour is invalid
Key Takeaway
🎯 Key Insight: Use a hash map to store positions for O(1) lookup, then validate each consecutive move follows the L-shaped pattern (|dr|=2,|dc|=1 or |dr|=1,|dc|=2)

Time & Space Complexity

Time Complexity
⏱️
O(n²)

Single pass to build map O(n²) + validate n² moves with O(1) lookups

n
2n
Quadratic Growth
Space Complexity
O(n²)

Hash map storing all n² positions

n
2n
Quadratic Space

Constraints

  • n == grid.length == grid[i].length
  • 3 ≤ n ≤ 7
  • 0 ≤ grid[i][j] < n2
  • All integers in grid are unique
  • Grid represents moves numbered from 0 to n² - 1
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
26.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen