FastPrepInsert Delete GetRandom O(1) - Duplicates Allowed

Insert Delete GetRandom O(1) - Duplicates Allowed

Bloomberg LP logoBloomberg LP● HardNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process commands on a multiset supporting average O(1) insertion, removal, and uniform random sampling over stored occurrences.

  • insert x adds one occurrence and returns true exactly when x was previously absent.
  • remove x removes one occurrence and returns whether removal occurred.
  • getRandom returns a uniformly random stored occurrence.

Return one string result per command. For deterministic judging, every getRandom command occurs when all stored occurrences have the same value.

Function

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

Examples

Example 1

operations = ["insert 1","insert 1","insert 2","remove 1","remove 2","getRandom"]return = ["true","false","true","true","true","1"]

One occurrence of 1 remains before getRandom.

Constraints

  • 1 <= operations.length <= 10^4.
  • Every getRandom is issued on a nonempty collection whose distinct-value count is one.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] processRandomizedCollection(String[] operations) {
  // Write your code here.
}
operations["insert 1","insert 1","insert 2","remove 1","remove 2","getRandom"]
expected["true", "false", "true", "true", "true", "1"]
Checking account…