Robot Survival Probability
Updated
Problem statement
A robot starts at cell (startRow, startCol) on a rectangular grid with rows rows and cols columns. Rows and columns are zero-indexed.
At every step, the robot independently chooses one of four moves with equal probability: up, down, left, or right. If a move takes it outside the grid, the robot is destroyed immediately.
Return the probability that the robot is still on the grid after exactly steps moves.
Function
robotSurvivalProbability(rows: int, cols: int, startRow: int, startCol: int, steps: int) → doubleExamples
Example 1
rows = 3cols = 3startRow = 1startCol = 1steps = 1return = 1.0From the center of a 3-by-3 grid, all four one-step moves remain inside.
Example 2
rows = 3cols = 3startRow = 0startCol = 0steps = 1return = 0.5From a corner, two of the four equally likely moves stay inside, so the survival probability is 2 / 4 = 0.5.
Example 3
rows = 2cols = 2startRow = 0startCol = 0steps = 2return = 0.25The robot has probability 1 / 2 of surviving each move from any corner of this grid, so the two-step probability is 1 / 4.
Example 4
rows = 1cols = 1startRow = 0startCol = 0steps = 0return = 1.0Before any move is made, the robot is still at its valid starting cell.
Constraints
1 <= rows, cols <= 500 <= startRow < rows0 <= startCol < cols0 <= steps <= 100- Answers within
10^-9of the expected value are accepted.
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
- Trace Every Water Drop to Its Resting DestinationGoogle · ONSITE INTERVIEWGoogle interview questions
- Multi-Head Self-Attention LayerOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Implement Circular PaddingApple · ONSITE INTERVIEWApple interview questions
- Grouped-Query Attention with a KV CacheAmazon · ONSITE INTERVIEW
- Two-Player Gambler's Ruin Probability and SimulationMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Scaled Dot-Product AttentionMeshy · OA
- Longest Border-Ending Diagonal PatternZipRecruiter · OAZipRecruiter interview questions
- Column and Row Parallel Linear LayersNVIDIA · PHONE SCREENNVIDIA interview questions