AO
Back

Closest Cake / Minimum Distance Between People and Cakes

Phone ScreenPhone ScreenSoftware Engineer, Machine Learning EngineerLast reported July 2026High Frequency

Problem Overview

You are given a 1D array where each element is 0 (empty), 1 (person), or 2 (cake).

Part 1 (base): Find the minimum distance between any person and any cake in the array. Some variants ask: given a specific person's index, find the nearest cake to that person.

Part 2 (follow-up / global assignment): Assign each person to exactly one cake so that the total (or maximum) distance is globally minimized — a bijective matching. Specifically, if multiple people are closest to the same cake, the cake is assigned to the person with the smallest distance; other people must find their next-best cake. The problem guarantees no two people are equidistant from the same cake and no two cakes are equidistant from the same person (so the assignment is unique). Given a specific person's index, determine which cake that person ends up eating under this globally optimal assignment.

Constraints discussed: O(n) time for Part 1. The array is 1D (some reports initially describe 2D but clarify it is 1D). Ties in distance are broken by smallest distance; no ties guaranteed in some problem variants.

Follow-up Arc

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

What are all the edge cases you need to handle?

Probes for: Candidate solves base problem
Trade-off discussion

Now consider global assignment: if multiple people want the same cake, it goes to the closest person. Given a specific person's index, which cake do they get under this globally optimal assignment? (when: Candidate solves Part 1 (minimum distance, single person to nearest cake))

Can you solve this in O(n) time? (when: Candidate solves with a simple approach (e.g., O(n log n) priority queue))

Can you optimize space complexity? Interviewer specifically mentioned DP approach.

Probes for: Candidate has a working solution

How would you implement this if elements arrive as a stream (online/streaming variant)?

Probes for: Candidate completes static array solution

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Priority Queue / Min-Heap for global assignmentEnumerate all person-cake distances, insert into a min-heap, then greedily pop and assign while skipping already-matched persons/cakes. Correct but O(P*C * log(P*C)) — worse than the sorted greedy O(n log n) approach.
DP + sliding windowOne report mentions interviewer pushed for DP optimization of the single-person closest-cake query, achieving O(n) space with DP + sliding window. Overkill for the base problem but may satisfy interviewers who specifically ask for DP framing.
Two-pass linear scan (left-to-right + right-to-left)For the per-person closest-cake query: scan left and right from person index, return the first cake found in either direction. Simple and O(n) but only solves Part 1, not global assignment.
BFS (for 2D variant)If the grid is 2D, multi-source BFS from all cakes simultaneously gives minimum distance to nearest cake for every cell. Correct for 2D but not the primary variant (which is 1D).

What Reports Emphasize

Common mistakes: Returning the naively closest cake to the given person without checking if another person has an even shorter distance to that same cake — this misses the global assignment requirement and was the reported reason candidates failed the follow-up.; Using a priority queue for the global assignment, which runs O(n log n) and fails the O(n) bar the interviewer asked for.; Spending too long debugging the base problem, leaving no time to attempt the global assignment follow-up.

Interviewer hints: When a candidate was explaining their approach, the interviewer directly interrupted to ask whether space complexity could be optimized using DP, steering toward a DP plus sliding window solution.; The problem statement guarantees no two people are equidistant from the same cake and no two cakes are equidistant from the same person, meaning the assignment is unique — interviewers stated this constraint explicitly to remove ambiguity.

What passers do: Candidates who passed recognized that even when given a single person's index, the global assignment must be solved for all people first before answering which cake that person gets.; Solving the base problem with two linear passes (left-to-right and right-to-left) was the expected O(n) approach and candidates who presented this did well.; Candidates who proactively wrote test cases and ran them in the provided IDE environment aligned with what the interviewer expected.

Why people fail: Failing the follow-up (global assignment) after passing Part 1 — this was explicitly cited as a reason for failure; Implementing naive greedy (nearest available cake per person) without recognizing it is suboptimal; Debugging too long on Part 1, running out of time for follow-ups; Unable to produce a correct solution even after extended discussion / 30-minute 'brainstorming' session with interviewer

Edge cases probed: Multiple people equidistant to the same cake — who gets it? (Problem statement guarantees this won't happen, but interviewer probed candidate's awareness); Array like {1, 1, 2, 2}: second person is closer to first cake, so first cake goes to second person; first person gets second cake — naive greedy fails; No people or no cakes in array; Person at boundary (index 0 or last index); Adjacent person and cake (distance = 1); All positions are the same type (all people, no cakes, or vice versa)

Practice

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

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 103 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?