Minimum Index Distance Between Person and Cake
Updated
Problem statement
You are given an array row of n cells. Each cell holds 0, 1 or 2:
0means the cell is empty.1means a person stands in the cell.2means a cake lies in the cell.
The distance between a person at index i and a cake at index j is |i - j|. Return the smallest distance over all pairs of one person and one cake.
If the row has no person or no cake, return -1.
For example, in [0, 1, 0, 2, 0] the person is at index 1 and the cake is at index 3, so the answer is 2.
Function
minDistance(row: int[]) → intExamples
Example 1
row = [0, 1, 0, 2, 0]return = 2The only person is at index 1 and the only cake is at index 3. The distance is |1 - 3| = 2. The answer is 2.
Constraints
1 <= row.length <= 2 * 10^5- Each
row[i]is 0, 1 or 2
More Snowflake Online Assessment questions
- Safest Shortest Path Through a GridOA · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- LFU CacheOA · ONSITE INTERVIEW · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Optimal TransferOA · Seen Oct 2026
- Generating Login CodesOA · Seen Sep 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Sep 2026
- Minimum Clicks Between Wiki PagesOA · PHONE SCREEN · Seen Sep 2026
- Horizontal Pod AutoscalerOA · Seen Jul 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
- Assign CookiesAmazon · PHONE SCREEN
- Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
- Meandering ArrayVerisk · OA
- Interleave Array HalvesAutodesk · OAAutodesk interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions