← 返回博客列表
Snowflake

Snowflake Interview Question: Cake Allocation in 1D Space

2025-11-12

Problem Description

Each person wants to eat one cake.

People tend to get the cake that is the closest to him/her. However, if he/she finds that this cake already belongs to someone else, the person will give up that cake and look for the next closest cake.

The number of cakes is never smaller than the number of people, so everyone is guaranteed to get a cake.

Suppose you are one of the people in the space. Given the array and your position, find out which cake belongs to you.

Example:

1 0 2 0 2 0 M 0

M (me) will get the cake at index 4.

Problem Analysis

Everyone tries to grab the closest cake. If the closest cake is taken, they look for the next closest. Since cakes >= people, everyone gets one.

Given your position, determine which cake you eventually get based on "nearest first, yield on conflict" rules.

Solution Implementation

Solution 1: Priority Queue (Optimized)

import heapq

def find_my_cake_optimized(space, my_position):
    """
    Optimized version using Priority Queue
    """
    n = len(space)
    
    # Find all people and cakes
    people = []
    cakes = []
    
    for i in range(n):
        if space[i] == 1 or space[i] == 'M':
            people.append(i)
        elif space[i] == 2:
            cakes.append(i)
    
    # Priority Queue for each cake: (distance, person_pos)
    cake_queues = {cake: [] for cake in cakes}
    
    for person in people:
        for cake in cakes:
            dist = abs(person - cake)
            heapq.heappush(cake_queues[cake], (dist, person))
    
    # Allocation process
    allocated = {}
    assigned_people = set()
    
    while len(assigned_people) < len(people):
        # For each cake, find the closest candidate
        for cake in cakes:
            if cake in allocated:
                continue
            
            while cake_queues[cake]:
                dist, person = heapq.heappop(cake_queues[cake])
                
                if person not in assigned_people:
                    # Check if this person has a closer available cake
                    has_closer = False
                    for other_cake in cakes:
                        if other_cake == cake or other_cake in allocated:
                            continue
                        if abs(person - other_cake) < dist:
                            has_closer = True
                            break
                    
                    if not has_closer:
                        allocated[cake] = person
                        assigned_people.add(person)
                        break
    
    # Return my cake
    for cake, person in allocated.items():
        if person == my_position:
            return cake
    
    return -1

Complexity Analysis

Summary

Snowflake Cake Allocation Problem covers:

  1. Greedy Algorithm: Nearest first strategy.
  2. Conflict Resolution: Handling competition.
  3. Simulation: Iterative allocation until stable.
  4. Stable Matching: Variation of Gale-Shapley.

oavoservice specializes in interview coaching for Snowflake / Google / Amazon, offering OA support and real-time VO assistance. Contact us if you need help.


Need real interview questions? Contact WeChat Coding0201 immediately to get real questions.