DoorDash OA 面试真题解析:Debugging Dasher Picking for Deliveries

24次阅读
没有评论

Debugging: Dasher Picking for Deliveries

Description

We have some code that attempts to assign a random dasher to a delivery:

  • There is a class Dasher that inherits from class User. All the users have a property id which is unique in the system, so let’s assume that there are no duplicated ids in Dasher objects.
  • There is a service DeliveryAssignmentService that hosts the dasher assignment logic.
    • The method addDasher can be called to add an available dasher to the system.
    • The field dasherMap contains all the dashers. The keys are consecutively increasing numbers starting from 0, which will be used for the method pickDasher() to pick a random Dasher object.
    • The method pickDasher will return a random dasher for the next delivery. The private method pickKey() will call object Random’s function nextInt() to get a random key from the consecutive key series. When a key is picked, the entry will be removed, and in the private method adjustMap() the map will move its largest key to fill the gap so that the keys are still consecutive from 0.
    • There is a remote service RemoteDeliveryRecordingService to be called when dasher adding or dasher picking happens.
    • The main() method gives you an example of how the code would be called.

However, unfortunately this code has logic bugs, bad designs, as well as some bad practices. Please try to find as many as you can, and then fix them. Logic bugs are more important as by fixing them we would have working code.

All the problems mentioned above are NOT language specific, so please do not worry about lacking knowledge of a specific language.

这道题考察的是对“随机分配 + 动态维护连续下标映射”这类代码的排错能力。核心思路是检查服务类在新增和抽取 dashers 时是否正确维护了 `dasherMap` 的连续键结构,尤其要关注随机键生成、删除元素后是否把最后一个元素搬到空位、以及远程记录服务调用时机是否正确。题目里还隐藏了不少工程性问题,比如空集合初始化、属性命名、`addDasher` / `pickDasher` 的边界处理,以及随机数范围和容器状态不一致导致的逻辑错误。

正文完
 0