Problem · Array

Random Dasher Set Operations

Learn this problem
MediumDoorDash logoDoorDashFULLTIMEONSITE INTERVIEW

Problem statement

Process a finite ordered array operations against one initially empty set of dasher IDs. Maintain both a dense array of IDs and a map from each ID to its current array index.

Each operation has one of these forms:

  • ADD id: if id is absent, append it to the array, record its index, and return true. If it is already present, leave the state unchanged and return false.
  • REMOVE id: if id is absent, leave the state unchanged and return false. Otherwise, move the last array element into the removed element's slot, update that moved element's map entry, delete the old last element, and return true.
  • PICK draw: if the set is empty, return null. Otherwise, return the ID at index floorMod(draw, size) in the current dense array.

Return one string result per operation, in order. Boolean results are the lowercase strings true and false.

Operational follow-ups

The judged operations are single-threaded and local. Be prepared to explain how serialized mutation or locking protects the array/map invariant, and how bounded timeouts, retries, and circuit breaking would isolate slow dependencies.

Function

runRandomDasherSet(operations: String[]) → String[]

Examples

Example 1

operations = ["ADD d1","ADD d2","PICK 0","REMOVE d1","PICK 0","REMOVE d1"]return = ["true","true","d1","true","d2","false"]

After removing d1, d2 moves into index 0. The second pick therefore returns d2.

Example 2

operations = ["ADD a","ADD b","ADD c","REMOVE b","PICK 1","REMOVE c","PICK -1","REMOVE a","PICK 7"]return = ["true","true","true","true","c","true","a","true","null"]

Removing b changes the dense array to [a, c]. A draw of -1 maps to index 0 when only a remains, and picking after the final removal returns null.

More DoorDash problems

drafts saved locally
public String[] runRandomDasherSet(String[] operations) {
    // Write your code here.
}
operations["ADD d1","ADD d2","PICK 0","REMOVE d1","PICK 0","REMOVE d1"]
expected["true", "true", "d1", "true", "d2", "false"]
checking account