Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

688. Knight Probability in Chessboard

Medium56.4% Acceptance
Dynamic Programming
Asked by:
A
Amazon
D
Directi
ProblemSolutions (12)VideosCompanies (6)Notes

Problem Statement

On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).

A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

The knight continues moving until it has made exactly k moves or has moved off the chessboard.

Return the probability that the knight remains on the board after it has stopped moving.

Example 1:

Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.

Example 2:

Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000

Constraints:

  • 1 <= n <= 25
  • 0 <= k <= 100
  • 0 <= row, column <= n - 1
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
E
Expedia Group
P
PhonePe
G
Goldman Sachs
+1

Approach

The Knight Probability in Chessboard problem asks for the probability that a knight remains on an n x n chessboard after making k moves from a starting position. The key observation is that a knight has 8 possible moves, and each move has equal probability.

This problem is best approached using Dynamic Programming. Define a DP state such as dp[m][r][c] representing the probability of the knight being on cell (r, c) after m moves. For each step, distribute the probability from the current cell to all valid knight moves. If a move goes outside the board, that probability is discarded.

You can implement this using top-down memoization or a bottom-up DP approach to iteratively update probabilities. Since each state considers up to 8 transitions, the overall time complexity becomes proportional to the number of moves and board cells.

This structured state transition makes the problem a classic example of probabilistic dynamic programming.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic Programming (Top-Down Memoization)O(k * n^2)O(k * n^2)
Dynamic Programming (Bottom-Up)O(k * n^2)O(n^2)

Video Solution Available

Pepcoding

View all video solutions

Solutions (12)

Dynamic Programming Approach

In this approach, we utilize a 3D array `dp` where `dp[k][i][j]` represents the probability that the knight is on position `(i, j)` after making `k` moves. Initially, the knight is placed at `(row, column)`, so `dp[0][row][column] = 1`. For each move `k`, we calculate the probability of the knight being on each cell `(i, j)` by summing over probabilities of positions that can move to `(i, j)` in one knight move.

This involves iterating through each cell and updating the probabilities according to knight's possible moves.

Time Complexity: O(k * n^2), as each position on the board and each move is processed.
Space Complexity: O(k * n^2), for storing the DP states.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3#define MAX 25
4
5double knightProbability(int n, int k, int row,     
    
    

Explanation

The code initializes a 3D dynamic array `dp` for storing the probabilities of being on each board position `(i, j)` after `k` moves. We iterate through each move and potential knight positions, updating probabilities based on possible knight moves.

Recursive Memoization Approach

This approach leverages recursion along with memoization to optimize calculations. We define a recursive function to calculate the probability the knight remains within bounds after `k` moves. We utilize memoization to cache results of subproblems to avoid redundant calculations. If the knight's position ends within the board boundaries, it contributes to the final probability.

Time Complexity: O(k * n^2).
Space Complexity: O(k * n^2).

CC++JavaPythonC#JavaScript
1






Video Solutions

Watch expert explanations and walkthroughs

Knights Probability in Chessboard Dynamic Programming | Leetcode-688 (Medium) Solution

Pepcoding
21:2920,425 views

Asked By Companies

6 companies
A
Amazon
D
Directi
E
Expedia Group
P
PhonePe
G
Goldman Sachs
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Palindromic SubstringMedium
Regular Expression MatchingHard
Generate ParenthesesMedium
Longest Valid ParenthesesHard
More similar problems

Related Topics

Dynamic Programming

Problem Stats

Acceptance Rate56.4%
DifficultyMedium
Companies6

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Knight Probability in Chessboard asked in FAANG interviews?

Yes, this problem is representative of the type of dynamic programming and probability-based questions sometimes asked in FAANG-style interviews. It tests state transitions, DP optimization, and careful handling of boundary conditions.

What data structure is best for Knight Probability in Chessboard?

A 2D or 3D dynamic programming array is typically used to store probabilities for board positions and move counts. Some optimized solutions use two 2D arrays to represent the current and next states, reducing space usage.

What is the optimal approach for Knight Probability in Chessboard?

The optimal approach uses dynamic programming to track the probability of the knight being on each cell after every move. By updating probabilities for all 8 possible knight moves at each step, we avoid redundant calculations and efficiently compute the final probability.

Why is dynamic programming suitable for Knight Probability in Chessboard?

Dynamic programming works well because the probability of reaching a cell after a certain number of moves depends on previously computed states. By storing and reusing these intermediate results, we avoid recomputation and significantly improve efficiency.

int
column
)
{
6
double
dp
[
k
+
1
]
[
MAX
]
[
MAX
]
;
7
int
directions
[
8
]
[
2
]
=
{
{
2
,
1
}
,
{
2
,
-
1
}
,
{
-
2
,
1
}
,
{
-
2
,
-
1
}
,
{
1
,
2
}
,
{
1
,
-
2
}
,
{
-
1
,
2
}
,
{
-
1
,
-
2
}
}
;
8
9
// Initialize
10
memset
(
dp
,
0
,
sizeof
(
dp
)
)
;
11
dp
[
0
]
[
row
]
[
column
]
=
1
;
12
13
// Fill DP table
14
for
(
int
m
=
1
;
m
<=
k
;
++
m
)
{
15
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
16
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
17
if
(
dp
[
m
-
1
]
[
i
]
[
j
]
>
0
)
{
18
for
(
int
d
=
0
;
d
<
8
;
++
d
)
{
19
int
ni
=
i
+
directions
[
d
]
[
0
]
;
20
int
nj
=
j
+
directions
[
d
]
[
1
]
;
21
if
(
ni
>=
0
&&
ni
<
n
&&
nj
>=
0
&&
nj
<
n
)
{
22
dp
[
m
]
[
ni
]
[
nj
]
+=
dp
[
m
-
1
]
[
i
]
[
j
]
/
8.0
;
23
}
24
}
25
}
26
}
27
}
28
}
29
30
// Sum up probabilities
31
double
result
=
0.0
;
32
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
33
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
34
result
+=
dp
[
k
]
[
i
]
[
j
]
;
35
}
36
}
37
return
result
;
38
}
#
include
<stdio.h>
2
#
include
<string.h>
3
4
#
define
MAX
25
5
6
double
memo
[
101
]
[
MAX
]
[
MAX
]
;
7
int
directions
[
8
]
[
2
]
=
{
{
2
,
1
}
,
{
2
,
-
1
}
,
{
-
2
,
1
}
,
{
-
2
,
-
1
}
,
{
1
,
2
}
,
{
1
,
-
2
}
,
{
-
1
,
2
}
,
{
-
1
,
-
2
}
}
;
8
9
// Recursive function with memoization
10
11
double
recurse
(
int
n
,
int
k
,
int
i
,
int
j
)
{
12
if
(
i
<
0
||
i
>=
n
||
j
<
0
||
j
>=
n
)
return
0
;
13
if
(
k
==
0
)
return
1
;
14
if
(
memo
[
k
]
[
i
]
[
j
]
!=
-
1.0
)
return
memo
[
k
]
[
i
]
[
j
]
;
15
16
double
prob
=
0.0
;
17
for
(
int
d
=
0
;
d
<
8
;
d
++
)
{
18
int
ni
=
i
+
directions
[
d
]
[
0
]
;
19
int
nj
=
j
+
directions
[
d
]
[
1
]
;
20
prob
+=
recurse
(
n
,
k
-
1
,
ni
,
nj
)
/
8.0
;
21
}
22
23
return
(
memo
[
k
]
[
i
]
[
j
]
=
prob
)
;
24
}
25
26
double
knightProbability
(
int
n
,
int
k
,
int
row
,
int
column
)
{
27
memset
(
memo
,
-
1
,
sizeof
(
memo
)
)
;
// Initialize memoization table
28
return
recurse
(
n
,
k
,
row
,
column
)
;
29
}

Explanation

C implementation uses recursion with memoization to solve the problem. The `memo` table caches results for specific `(i, j, k)` states to avoid redundant computations.