Problem · Hash Table

Set Command Simulation

Learn this problem
MediumPure Storage logoPure StorageFULLTIMEONSITE INTERVIEW

Problem statement

Simulate a set of strings over a finite batch of commands in operations.

  • ["PUT", value] inserts value. Inserting an existing value has no effect.
  • ["LOOKUP", value] emits "true" when the value is present and "false" otherwise.
  • ["REMOVE", value] removes the value when present and otherwise has no effect.
  • ["CLEAR"] removes every value.
  • ["ITERATE"] emits all current values in ascending lexicographic order, joined with commas. It emits the empty string when the set is empty.

Only LOOKUP and ITERATE emit outputs. Return those strings in command order.

Function

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

Examples

Example 1

operations = [["PUT","pear"],["PUT","apple"],["LOOKUP","pear"],["ITERATE"],["REMOVE","pear"],["LOOKUP","pear"],["CLEAR"],["ITERATE"]]return = ["true","apple,pear","false",""]

The first lookup succeeds. Iteration sorts apple before pear; after removal and clear, the later outputs show the updated state.

Example 2

operations = [["PUT","b"],["PUT","b"],["PUT","a"],["ITERATE"],["REMOVE","missing"],["LOOKUP","b"]]return = ["a,b","true"]

Duplicate insertion and removal of an absent value do not change the set.

Constraints

  • 1 <= operations.length <= 200000.
  • Every command is one of PUT, LOOKUP, REMOVE, CLEAR, or ITERATE and has the documented arity.
  • Every value has between 1 and 30 ASCII letters, digits, or underscores.
  • The total number of characters across all commands is at most 2000000.

More Pure Storage problems

drafts saved locally
public String[] runSetCommands(String[][] operations) {
    // Write your code here.
}
operations[["PUT","pear"],["PUT","apple"],["LOOKUP","pear"],["ITERATE"],["REMOVE","pear"],["LOOKUP","pear"],["CLEAR"],["ITERATE"]]
expected["true", "apple,pear", "false", ""]
checking account