Knight Probability in Chessboard

Calculate the probability that a knight remains on an N x N chessboard after making K moves. Initially, the knight is placed at a given position on the board. It can move in any of the 8 directions it is allowed to on a chessboard.

The knight's potential moves from any position (i, j) are: (i + 2, j + 1), (i + 2, j - 1), (i - 2, j + 1), (i - 2, j - 1), (i + 1, j + 2), (i + 1, j - 2), (i - 1, j + 2), and (i - 1, j - 2).

Input:

The first line contains an integer, 'T', denoting the number of test cases. Each test case starts with two integers, 'N' and 'K', which denote the chessboard's dimension and the number of moves the knight must make. The next line of each test case contains two integers, 'Sx' and 'Sy', representing the knight's initial position.

Output:

For each test case, print the probability that the knight remains on the chessboard after all moves. Ensure the output is accurate to six decimal places.

Example:

Input:
2
8 3
0 0
10 7
5 5
Output:
0.125000
0.981250

Constraints:

  • 1 <= T <= 5
  • 0 <= N <= 30
  • 0 <= K <= 500
  • 0 <= Sx, Sy <= N - 1

Time Limit: 1 sec.

Note:

Consider all possible sequences of the knight's moves to determine its likelihood of staying on the board.

AnswerBot
4mo

Calculate the probability that a knight remains on an N x N chessboard after making K moves.

  • Use dynamic programming to calculate the probability of the knight staying on the board after each move.

  • Cons...read more

Help your peers!
Select
Add answer anonymously...

Expedia Group Software Developer interview questions & answers

A Software Developer was asked Q. Knight Probability in Chessboard Calculate the probability that a knight remains...read more
A Software Developer was asked Q. Remove Character from String Problem Statement Given a string str and a characte...read more
A Software Developer was asked Q. Binary Tree to Doubly Linked List Transform a given Binary Tree into a Doubly Li...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Knight Probability in Chessboard Calculate the probability that a knight remains...read more
A Software Developer was asked Q2. Remove Character from String Problem Statement Given a string str and a characte...read more
A Software Developer was asked Q3. Binary Tree to Doubly Linked List Transform a given Binary Tree into a Doubly Li...read more
Expedia Group Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits