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.
This problem asks you to debug a random-assignment service that stores dashers in a map with consecutive integer keys. The main challenge is to keep the keys compact after removing a randomly picked dasher, so the implementation must correctly handle random index generation, deletion, and map rebalancing. In addition to logic bugs, the code also contains design and maintainability issues such as improper initialization, weak separation of concerns, and unsafe handling of empty collections.