Problem · Hash Table

Indexed In-Memory Table

Learn this problem
MediumRetell AI logoRetell AIFULLTIMEPHONE SCREEN

Problem 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:value inserts a row and outputs true. If id already exists, it changes nothing and outputs false.
  • UPDATE:id:tag:value replaces the tag and value of an existing row and outputs true. If id is absent, it outputs false.
  • DELETE:id removes an existing row and outputs true. If id is absent, it outputs false.
  • FIND:tag outputs 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 id is between 0 and 10^9.
  • Each tag and value contains 1 to 32 ASCII letters, digits, underscores, or hyphens and contains no colon.
drafts saved locally
public String[] runIndexedTable(String[] operations) {
  // Write your code here.
}
operations["INSERT:2:blue:x","INSERT:1:blue:y","FIND:blue","UPDATE:2:red:z","FIND:blue","DELETE:1","FIND:blue"]
expected["true", "true", "1", "2", "true", "1", "true", ""]
checking account