Minimum Index Distance Between Person and Cake
Reported by candidates from Snowflake's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're looking at a Snowflake OA from May 2026 that sounds simple but trips up candidates who overthink it. Given a ternary array of 0s (empty), 1s (person), and 2s (cake), find the minimum index distance between any person and any cake. Return -1 if either is missing. The trap: candidates scan left-to-right or use nested loops when a single pass with two pointers solves it in linear time. StealthCoder will pull the pattern instantly if you blank on the live OA.
The problem
Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Problem Minimum Index Distance Between Person and Cake in a Ternary Array Given a 1D array A of length n where each element is in {0,1,2}: 0 means empty 1 means person 2 means cake Compute the minimum distance between any person (1) and any cake (2) in terms of index difference: [ \min_{i,j} |i-j| \quad \ ext{where } A[i]=1, A[j]=2 ] If there is no valid pair (the array does not contain both 1 and 2), output -1. Input Line 1: integer n Line 2: n integers describing A Output One line: the minimum distance, or -1 Constraints 1 <= n <= 2*10^5 Sample Tests (5) Input: 5 0 1 0 2 0 Output: 2 Input: 6 1 0 0 0 2 0 Output: 4 Input: 6 1 0 2 0 2 1 Output: 1 Input: 4 0 0 0 0 Output: -1 Input: 3 2 0 2 Output: -1 Example Input 5 0 1 0 2 0 Output 2 Function Description Complete solveMinimumPersonCakeDistance. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string array must match the expected standard output lines for the sample input. Use the limits and requirements stated in the prompt.Reported by candidates. Source: FastPrep
Pattern and pitfall
The algorithmic core is straightforward: track the most recent person index and most recent cake index as you iterate once through the array. Every time you see a 1 or 2, update its respective 'last seen' variable and compute the distance between them. Keep the minimum. Edge case: if you finish and never saw both a 1 and a 2, return -1. The trick is resisting the urge to collect all indices first or nest two loops. A single pass with O(n) time and O(1) space is optimal. Common pitfall: forgetting to initialize or returning distance before confirming both types exist. StealthCoder flags this pattern instantly and enforces the right initialization order during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Index Distance Between Person and Cake cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Snowflake's OA.
Snowflake reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Index Distance Between Person and Cake FAQ
Do I need to store all person and cake positions?+
No. Store only the most recent index of each type. Every new person or cake updates one variable and lets you compute a candidate distance. Single pass, constant space. That's the efficiency win.
What if there are multiple people or multiple cakes?+
Doesn't matter. You only care about the closest pair. Tracking the last-seen index of each type ensures you capture every possible distance without redundant work.
How do I handle the -1 case without bloating my code?+
Initialize both 'last person' and 'last cake' to -1. After one pass, if either is still -1, return -1. Otherwise, return the minimum distance you computed.
Is this problem likely to reappear at other companies?+
Yes. It's a canonical two-pointer/single-pass problem. You'll see variants on HackerRank, LeetCode, and other OA platforms. The pattern (track state, update on condition, compute metric) is durable.
What's the common mistake candidates make under time pressure?+
Nested loops to compare all pairs, which is O(n^2) and fails on large inputs. Or forgetting to initialize trackers, which leads to index out-of-bounds crashes. Both are easy to catch with a single pass.