Knight Exit Probability
Updated
Problem statement
A knight starts at position (row, column) on an n x n chessboard. Rows and columns are zero-indexed.
The knight attempts exactly k moves. On every move it chooses each of its eight standard knight moves with equal probability. If a chosen move lands outside the board, the knight leaves the board and remains outside for all remaining moves.
Return the probability that the knight has left the board after the k attempted moves. Answers within 10^-6 of the exact value are accepted.
Function
knightExitProbability(n: int, k: int, row: int, column: int) → doubleExamples
Example 1
n = 3k = 2row = 0column = 0return = 0.9375Only two of the eight first moves stay on the board. From either surviving square, two of the next eight moves stay on the board, so the survival probability is 4 / 64 and the exit probability is 60 / 64 = 0.9375.
Example 2
n = 1k = 1row = 0column = 0return = 1.0Every knight move from the only square leaves the board.
Constraints
1 <= n <= 250 <= k <= 1000 <= row, column < n
More InMobi Onsite Interview questions
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
- Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
- Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
- Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
- Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
- Word SquaresONSITE INTERVIEW · Seen Jan 2022
- Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
- Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
- Trapping Rain WaterONSITE INTERVIEW · Seen May 2021
Similar questions at other companies
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- 3D Grid Diagonal Walk ProbabilityApple · PHONE SCREENApple interview questions
- Count Valid A-B-C Sequences Under a Modulo-Four RuleInfosys · OAInfosys interview questions
- Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Count Divisible Power SumsGoodScore · ONSITE INTERVIEW
- Maximum All-Ones Square Side LengthOmnissa · ONSITE INTERVIEWOmnissa interview questions