Debugging: Dasher Picking for Deliveries
Description
We have some code that attempts to assign a random dasher to a delivery:
- There is a class
Dasherthat inherits from classUser. All the users have a propertyidwhich is unique in the system, so let’s assume that there are no duplicated ids in Dasher objects. - There is a service
DeliveryAssignmentServicethat hosts the dasher assignment logic.- The method
addDashercan be called to add an available dasher to the system. - The field
dasherMapcontains all the dashers. The keys are consecutively increasing numbers starting from 0, which will be used for the methodpickDasher()to pick a randomDasherobject. - The method
pickDasherwill return a random dasher for the next delivery. The private methodpickKey()will call object Random’s functionnextInt()to get a random key from the consecutive key series. When a key is picked, the entry will be removed, and in the private methodadjustMap()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
RemoteDeliveryRecordingServiceto be called when dasher adding or dasher picking happens. - The
main()method gives you an example of how the code would be called.
- The method
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` 的边界处理,以及随机数范围和容器状态不一致导致的逻辑错误。