Airbnb questions

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) stores value in the object's field, replacing any value the field already had, and returns null. The object is created if it does not exist.
  • get(object_id, field) returns the field's value, or null when the object or the field does not exist.
  • delete_value(object_id, field) deletes one field and returns true, or returns false when the object or the field does not exist.
  • delete_object(object_id) deletes the object with all its fields and returns true, or returns false when the object does not exist.

An object exists while it holds at least one field. Ids and field names are case sensitive.

  1. 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: set stores two fields on user1, and the second set of name replaces alice with bob. get of a field or an object that was never set is null. delete_value returns true the first time it removes nick and false the second time. delete_object returns false for user2, which has no fields, and true for user1, after which every field of it is gone and a second delete returns false.

Constraints

  • 1 <= len(object_id), len(field), len(value) <= 32, all ASCII letters and digits
  • At most 2000 calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question