This question evaluates proficiency in implementing stateful in-memory data structures, record-level exclusive locking semantics, concurrent access control, and efficient string-keyed CRUD operations.
Implement an in-memory key–record database that supports basic CRUD on fields plus an exclusive lock per record.
You are given a sequence of queries. Each query is an array of strings. Implement a function that processes all queries in order and returns an array of strings containing the outputs of the queries that produce output.
recordKey
(string).
field
(string) to
value
(string).
userId
, string).
userId
performing the operation.
Each query is one of:
SET recordKey field value userId
recordKey[field] = value
(creating the record if needed) and output
"OK"
.
"LOCKED"
and do not change the database.
GET recordKey field
""
.
DELETE recordKey field userId
"true"
.
"false"
.
"LOCKED"
.
LOCK recordKey userId
"INVALID"
.
userId
and output
"ACQUIRED"
.
userId
, output
"ACQUIRED"
(idempotent).
"WAIT"
.
UNLOCK recordKey userId
"INVALID"
.
userId
, unlock it and output
"RELEASED"
.
"RELEASED"
(idempotent).
"INVALID"
.
If a record A is locked by user u1, then SET A x 5 u2 must output "LOCKED", while GET A x still returns the stored value (or empty string if missing).