FastPrep3D Grid Diagonal Walk Probability
Problems/3D Grid Diagonal Walk Probability

3D Grid Diagonal Walk Probability

Apple logoApple● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time phone screen. The interviewer gave an 8x8x8 3D grid where valid coordinates are 0 <= x, y, z < 8. You start at (x, y, z) and take exactly N steps, with N < 20. Each step goes 1 unit along one of the 8 diagonal directions of 3D space, all equally likely. You find the chance of still being inside the grid after N steps.

The report does not say whether a walker that leaves the grid can come back. Here, a step outside ends the walk, which matches the probability DP the candidate described. It also gives no lower limit for N, so n = 0 is allowed and returns 1. A diagonal step here changes all three coordinates by 1. Answers are accepted within 10^-9.

Our version is about 95% the same as the reported question.

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) → double

Examples

Example 1

x = 0y = 0z = 0n = 1return = 0.125

From 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.0

The 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.25

On 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 <= 7
  • 0 <= 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)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public double diagonalWalkProbability(int x, int y, int z, int n) {
        // Write your code here.
    }
}
Checking account…