AO
Back

Closest Cake / Minimum Distance Between People and Cakes

Also reported as Closest Cake Problem · Assign Cakes to People · Nearest Cake in a 1D Array

Phone ScreenPhone ScreenLast reported July 2026High Frequency

Problem Overview

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.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 3
Trade-off discussion

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.)

Probes for: While explaining the approach

How would you handle the array arriving as a stream?

Probes for: After a working solution

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Min-heap over every (distance, person, cake) pairThe 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 indexTwo 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.

What Reports Emphasize

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).

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Snowflake with friends?

Send them this page. It is free to read, no account needed.

More Snowflake Questions

Free preview

Every question in the Snowflake catalog gets this depth

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.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Snowflake · Phone Screen · Reported 12× across candidate reports
Is this helpful?