Problem · Hash Table
Indexed In-Memory Table
Learn this problemProblem statement
Execute the commands in operations against an in-memory table. Each row has a unique integer id, a string tag, and a string value. The table maintains an equality index on tag.
INSERT:id:tag:valueinserts a row and outputstrue. Ifidalready exists, it changes nothing and outputsfalse.UPDATE:id:tag:valuereplaces the tag and value of an existing row and outputstrue. Ifidis absent, it outputsfalse.DELETE:idremoves an existing row and outputstrue. Ifidis absent, it outputsfalse.FIND:tagoutputs matching row IDs in ascending order using bracket notation such as[1,4]. It outputs[]when no row has that tag.
Return one string output for every command, in command order. Every lookup observes all preceding mutations.
Function
runIndexedTable(operations: String[]) → String[]Examples
Example 1
operations = ["INSERT:2:blue:x","INSERT:1:blue:y","FIND:blue","UPDATE:2:red:z","FIND:blue","DELETE:1","FIND:blue"]return = ["true","true","[1,2]","true","[1]","true","[]"]The first lookup uses the secondary index to return IDs 1 and 2 in sorted order. Updating row 2 removes it from the blue index, and deleting row 1 leaves that index empty.
Example 2
operations = ["INSERT:7:a:v","INSERT:7:b:w","UPDATE:9:a:x","DELETE:9","FIND:a"]return = ["true","false","false","false","[7]"]Duplicate insertion and mutations of missing IDs fail without changing the existing row.
Constraints
1 <= operations.length <= 100000.- Each command has exactly one documented format.
- Every
idis between0and10^9. - Each
tagandvaluecontains 1 to 32 ASCII letters, digits, underscores, or hyphens and contains no colon.