Random Dasher Set Operations
Learn this problemProblem 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: ifidis absent, append it to the array, record its index, and returntrue. If it is already present, leave the state unchanged and returnfalse.REMOVE id: ifidis absent, leave the state unchanged and returnfalse. 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 returntrue.PICK draw: if the set is empty, returnnull. Otherwise, return the ID at indexfloorMod(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.