Review Constant-Time Random Courier Selection and Removal

Read the full interview experience this question came from →

Quick Overview

Maintain dense map indices for uniform random selection and swap-with-last removal, including empty states, concurrency, and complexity checks.

Review Constant-Time Random Courier Selection and Removal

Company: DoorDash

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Review a delivery-assignment service that stores couriers in `dasher_map` under contiguous integer keys starting at `0`. `add_dasher` inserts a courier. `pick_dasher` chooses a courier at random and removes it. `adjust_map` restores contiguous keys by moving the last entry into a removed slot. Explain the invariants and constant-time operations needed for this design, the bugs you would look for, and the code-quality improvements you would request in a review. ### Requirements and Constraints - After each operation, the map keys must be exactly `0` through `n - 1`, where `n` is the number of stored entries. - Selection should be uniform over the currently stored entries. - Preserve the original operations' expected or amortized constant-time behavior; do not repair the map by scanning and renumbering every entry. - An external delivery-recording service can be assumed to work correctly. - No actual implementation is supplied here, so identify failure modes and required behavior rather than claiming a particular unseen line is wrong. ### Clarifying Questions - What should selection do when the map is empty? - May the same courier identity be added more than once, or must insertion reject duplicates? - Can calls occur concurrently, and must selecting and removing a courier be one atomic operation? - Which method owns the size update, and when is the selected courier sent to the recording service? ```hint Save the selected entry before replacing its slot The last entry may move into the selected index. The courier returned or recorded must still be the one originally selected. ``` ### What a Strong Answer Covers - A precise dense-key invariant and correct random-index bounds. - Append-on-add and swap-with-last removal, including selection of the last entry itself. - Empty-map behavior, duplicate-identity policy, and avoidance of inconsistent size bookkeeping. - Expected or amortized complexity and concurrency requirements where applicable. - Tests for structural correctness and random selection, plus clear method contracts and naming. ### Follow-up Questions 1. What is different when the randomly selected key is already the final key? 2. Why can moving the last entry preserve uniform selection on the next call? 3. How could a duplicate courier identity change the meaning of uniform selection?

Overview: Maintain dense map indices for uniform random selection and swap-with-last removal, including empty states, concurrency, and complexity checks.

Read the full DoorDash Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/DoorDash
DoorDash logo
DoorDash
Sep 5, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

Review a delivery-assignment service that stores couriers in dasher_map under contiguous integer keys starting at 0. add_dasher inserts a courier. pick_dasher chooses a courier at random and removes it. adjust_map restores contiguous keys by moving the last entry into a removed slot.

Explain the invariants and constant-time operations needed for this design, the bugs you would look for, and the code-quality improvements you would request in a review.

Requirements and Constraints

  • After each operation, the map keys must be exactly 0 through n - 1 , where n is the number of stored entries.
  • Selection should be uniform over the currently stored entries.
  • Preserve the original operations' expected or amortized constant-time behavior; do not repair the map by scanning and renumbering every entry.
  • An external delivery-recording service can be assumed to work correctly.
  • No actual implementation is supplied here, so identify failure modes and required behavior rather than claiming a particular unseen line is wrong.

Clarifying Questions Guidance

  • What should selection do when the map is empty?
  • May the same courier identity be added more than once, or must insertion reject duplicates?
  • Can calls occur concurrently, and must selecting and removing a courier be one atomic operation?
  • Which method owns the size update, and when is the selected courier sent to the recording service?

What a Strong Answer Covers Guidance

  • A precise dense-key invariant and correct random-index bounds.
  • Append-on-add and swap-with-last removal, including selection of the last entry itself.
  • Empty-map behavior, duplicate-identity policy, and avoidance of inconsistent size bookkeeping.
  • Expected or amortized complexity and concurrency requirements where applicable.
  • Tests for structural correctness and random selection, plus clear method contracts and naming.

Follow-up Questions Guidance

  1. What is different when the randomly selected key is already the final key?
  2. Why can moving the last entry preserve uniform selection on the next call?
  3. How could a duplicate courier identity change the meaning of uniform selection?
Loading comments...