Snowflake 面试题:在一维空间中分配蛋糕,确定你最终拿到哪块蛋糕

51次阅读
没有评论

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 0M (me) will get the cake at index 4.

每个人都会去抢离自己最近的蛋糕。如果最近的蛋糕被别人占了,就继续找下一个最近的。因为蛋糕数量 ≥ 人数,所以每个人最终都能分到蛋糕。题目给了你的位置,需要根据“最近优先、冲突让位”的规则算出你最终会拿到哪个蛋糕。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 SnowFlake、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0