3D Grid Diagonal Walk Probability
Updated
Problem statement
A walker moves on an 8 x 8 x 8 grid. A position (x, y, z) is inside the grid when each coordinate is at least 0 and less than 8.
The walker starts at (x, y, z) and takes exactly n steps. On each step, it picks one of the 8 diagonal directions with equal probability. A diagonal step adds +1 or -1 to each of the three coordinates.
If a step lands outside the grid, the walk ends there and the walker cannot step back in. Return the probability that the walker is still inside the grid after all n steps. Answers within 10^-9 of the exact value are accepted.
Function
diagonalWalkProbability(x: int, y: int, z: int, n: int) → doubleExamples
Example 1
x = 0y = 0z = 0n = 1return = 0.125From the corner (0,0,0), only the move (+1,+1,+1) stays inside. That is 1 of the 8 directions, so the probability is 1/8 = 0.125.
Example 2
x = 3y = 4z = 2n = 2return = 1.0The start is at least 2 away from every wall on every axis, so no two-step walk can leave the grid. The probability is 1.
Example 3
x = 0y = 3z = 7n = 2return = 0.25On the x axis, the first step must be +1, which has probability 1/2; the second step can then go either way. On the z axis, the first step must be -1, also 1/2. The y axis cannot leave in two steps. The result is 1/2 * 1 * 1/2 = 0.25.
Constraints
0 <= x, y, z <= 70 <= n <= 19
More Apple Phone Screen questions
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
- Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
- Run-Length DecodingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Robot Survival ProbabilityInMobi · ONSITE INTERVIEWInMobi interview questions
- Count Valid A-B-C Sequences Under a Modulo-Four RuleInfosys · OAInfosys interview questions
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Unique Grid PathsMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
- Count Starts Reaching a Grid MaximumDuolingo · PHONE SCREENDuolingo interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Factory Cost — Minimum Cost Across StagesStripe · PHONE SCREENStripe interview questions
- Path Through an O/X Grid Using Only Right and DownAmazon · PHONE SCREEN
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)