In-Memory Object Manager
AirbnbOnline assessmentMedium
You are building an in-memory object manager as the class ObjectManager. Each object, identified by a string id, holds named fields, each with a string value.
Part 1: Create, read and delete
Implement:
set(object_id, field, value)storesvaluein the object's field, replacing any value the field already had, and returnsnull. The object is created if it does not exist.get(object_id, field)returns the field's value, ornullwhen the object or the field does not exist.delete_value(object_id, field)deletes one field and returnstrue, or returnsfalsewhen the object or the field does not exist.delete_object(object_id)deletes the object with all its fields and returnstrue, or returnsfalsewhen the object does not exist.
An object exists while it holds at least one field. Ids and field names are case sensitive.
- Example 1
- init
[]- operations
[["set",["user1","name","alice"]],["set",["user1","nick","ali"]],["get",["user1","name"]],["get",["user1","age"]],["get",["user2","name"]],["set",["user1","name","bob"]],["get",["user1","name"]],["delete_value",["user1","nick"]],["delete_value",["user1","nick"]],["get",["user1","nick"]],["delete_object",["user2"]],["delete_object",["user1"]],["get",["user1","name"]],["delete_object",["user1"]]]- Output
[null,null,"alice",null,null,null,"bob",true,false,null,false,true,null,false]
Why:
setstores two fields onuser1, and the secondsetofnamereplacesalicewithbob.getof a field or an object that was never set isnull.delete_valuereturnstruethe first time it removesnickandfalsethe second time.delete_objectreturnsfalseforuser2, which has no fields, andtrueforuser1, after which every field of it is gone and a second delete returnsfalse.
Constraints
1 <= len(object_id), len(field), len(value) <= 32, all ASCII letters and digits- At most
2000calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.