def nearest_cake_distance(arr: list[int], person_idx: int) -> int:
Snowflake's most reported phone-screen question ("the cake question"): twelve first-hand reports from 2025 and 2026, most of them phone screens. The array is one-dimensional: 0 is an empty spot, 1 a person, 2 a cake. Several reports add a follow-up in which every person eats a cake and you must say which cake one given person ends up with.
What the tests call (names and signatures are our setup; ask your interviewer).
def min_person_cake_distance(arr: list[int]) -> int: ... # Part 1
def nearest_cake_distance(arr: list[int], person_idx: int) -> int: ... # Part 1
def assign_cake_for_person(arr: list[int], person_idx: int) -> int: ... # Part 2
Distance is the difference of the two indices. The tests always include at least one person and one cake, so what you return when either is missing is up to you.
Part 1 — Closest cake
min_person_cake_distance(arr) returns the smallest distance between any person and any cake. One report says the interviewer required O(n) time. Example: [0, 1, 0, 0, 2, 0, 1, 2] returns 1 (the person at 6 and the cake at 7).
The other reported form of Part 1 gives you one person: nearest_cake_distance(arr, person_idx) returns how far that person is from their nearest cake, looking in both directions. Example: nearest_cake_distance([2, 0, 0, 1, 0, 2], 3) returns 2 (the cake at 5).
Not graded
Reported follow-ups you may be asked to talk through: whether space can be reduced (one interviewer raised DP), how you would answer when the array arrives as a stream (one report precomputed the nearest cake per index first), and edge cases.
Reported in several rounds. Even for one person you must work out the whole assignment: another person may be closer to that person's nearest cake. The reports give the rule only for a contested cake: it goes to the closest person. A complete procedure, and the one this page's tests use, is closest pair first: take the closest (person, cake) pair still free, assign it, remove both, repeat. That is our reading, so confirm it. One report guarantees no tied distances. On [1, 1, 2, 2] the second person gets the first cake and the first person the second. One candidate used a priority queue over every person-cake distance with sets of taken people and cakes. A reader's suggestion to pair people and cakes in sorted order minimises total distance instead, which is a different rule and gives the other answer on [1, 1, 2, 2].
Reported once, without detail beyond the candidate's answer of DP plus a sliding window. The one-pass scan that remembers only the last person and the last cake already uses O(1) extra space.
Reported once, after the candidate had written a version that precomputes the nearest cake for each index and discussed corner cases; the report gives no further detail.
From candidate reports: twelve first-hand reports, nearly all of them phone screens, from 2025 through mid-2026. Several call it "the classic cake question" and say it is in the forum's question bank. The base question is reported in two forms: the minimum distance between any person and any cake (one report says O(n) was required), and the distance from one given person to their nearest cake. One report says the interviewer asked, while the approach was being explained, whether DP could reduce the space. Another discussed corner cases and then how to handle the array as a stream, after precomputing the nearest cake for each index.
The follow-up is where reports say they struggled. One candidate writes that the follow-up is what they failed, and spells out the rule: even for one person you need everyone's assignment, and on [1, 1, 2, 2] the second person takes the first cake. Another says they did not know how to do it and the interviewer did not help much. One who solved it used a priority queue over every person-cake distance plus two sets of taken people and cakes. One report says you write and run your own test cases in an IDE the interviewer provides.
Also reported as Closest Cake Problem · Assign Cakes to People · Nearest Cake in a 1D Array
Snowflake's most reported phone-screen question ("the cake question"): twelve first-hand reports from 2025 and 2026, most of them phone screens. The array is one-dimensional: 0 is an empty spot, 1 a person, 2 a cake. Several reports add a follow-up in which every person eats a cake and you must say which cake one given person ends up with.
What the tests call (names and signatures are our setup; ask your interviewer).
def min_person_cake_distance(arr: list[int]) -> int: ... # Part 1
def nearest_cake_distance(arr: list[int], person_idx: int) -> int: ... # Part 1
def assign_cake_for_person(arr: list[int], person_idx: int) -> int: ... # Part 2
Distance is the difference of the two indices. The tests always include at least one person and one cake, so what you return when either is missing is up to you.
Part 1 — Closest cake
min_person_cake_distance(arr) returns the smallest distance between any person and any cake. One report says the interviewer required O(n) time. Example: [0, 1, 0, 0, 2, 0, 1, 2] returns 1 (the person at 6 and the cake at 7).
The other reported form of Part 1 gives you one person: nearest_cake_distance(arr, person_idx) returns how far that person is from their nearest cake, looking in both directions. Example: nearest_cake_distance([2, 0, 0, 1, 0, 2], 3) returns 2 (the cake at 5).
Part 2 — Who eats which cake
Each person goes for the cake nearest to them, and the reports give the conflict rule: a cake that is the nearest for several people goes to the closest of them. They do not spell out what happens next to the people who lose, so the tests use our reading: repeatedly take the closest (person, cake) pair among those still free, give that cake to that person, remove both, and repeat. It agrees with the reported example and with every test below; confirm the rule with your interviewer. assign_cake_for_person(arr, person_idx) returns the index of the cake that person ends up with. The reports do not give the counts; every test has at least as many cakes as people.
One report gives two guarantees: no two people are the same distance from one cake, and no two cakes are the same distance from one person, so the answer is unique. Another says only that people competing for one cake are never at the same distance. The tests respect both. Even though you are asked about one person, someone else may take that person's nearest cake first, so you need the whole assignment.
Example, from one report: [1, 1, 2, 2]. The second person is closest to the first cake (distance 1), so that cake is theirs, and the first person gets the second cake: assign_cake_for_person([1, 1, 2, 2], 0) returns 3 and assign_cake_for_person([1, 1, 2, 2], 1) returns 2. This rule is not the same as minimising the total distance: on this array both pairings total 4.
Not graded
Reported follow-ups you may be asked to talk through: whether space can be reduced (one interviewer raised DP), how you would answer when the array arrives as a stream (one report precomputed the nearest cake per index first), and edge cases.
Every person now goes for their nearest cake, and a contested cake goes to the closest person. Given one person's index, which cake do they end up with? (when: After the base question (closest cake))
Can you reduce the space complexity? (One interviewer raised DP.)
How would you handle the array arriving as a stream?
| Approach | Notes |
|---|---|
| Min-heap over every (distance, person, cake) pair | The same closest-pair-first rule; one candidate used a priority queue plus sets of taken people and cakes. O(P*C log(P*C)). |
| Sorted pairing (i-th person with i-th cake) | A reader's suggestion: O(n) and minimises total distance when counts are equal, but it is a different rule and gives the other answer on the reported [1, 1, 2, 2]. |
| Precompute the nearest cake for every index | Two passes record the index of the nearest cake for every position, so any one-person query is O(1); one report found the nearest target's index for every position this way before discussing a streaming version. |
Common mistakes: Answering the follow-up with the queried person's nearest cake without checking whether a closer person takes it; one report says this is the trap.; Our recommendation: do not swap in the sorted pairing (i-th person with i-th cake); it minimises total distance and gives the opposite of the reported answer on [1, 1, 2, 2].; Comparing every person with every cake in the base question; one report says O(n) was required.
Interviewer hints: One interviewer asked, while the candidate was explaining the approach, whether DP could reduce the space.; One report states the follow-up's guarantee: no two people are equally far from one cake and no two cakes are equally far from one person, so the assignment is unique. Another says only that people competing for one cake are never equally far from it.
What passers do: Our recommendation: for the follow-up, solve the whole assignment before answering for one person; one report says this is required even when only one index is given.; Our recommendation: write and run your own tests; one report says the candidate had to write test cases and run them in an IDE the interviewer provided.
Why people fail: One candidate says they failed the follow-up (the global assignment) after an easy base question.; One candidate says they could not solve the follow-up and the round turned into thirty minutes of brainstorming with little help from the interviewer.
Edge cases probed: [1, 1, 2, 2]: the second person is closest to the first cake, so the first person gets the second cake (reported example).; The queried person's nearest cake is taken by someone closer, so they get a cake on the other side.; More cakes than people (not reported; our test).; The closest cake can be to a person's left or right (our test).
Send them this page. It is free to read, no account needed.
What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 86 Snowflake questions, refreshed monthly from new candidate reports.