Airbnb questions

Parcel Attribute Store

AirbnbOnline assessmentMedium

You are building an in-memory store of parcel attributes as the class ParcelStore. Every method takes the call's timestamp as its first argument.

Part 1: Set, get and conditional update

Implement:

  • set(timestamp, parcel_id, name, value) stores the attribute name of the parcel with the given value, replacing any value it already had. It returns null.
  • get(timestamp, parcel_id, name) returns the attribute's value, or null when the parcel or the attribute does not exist.
  • update_if_match(timestamp, parcel_id, name, expected, value) replaces the attribute's value with value and returns true, only when the attribute exists and its current value equals expected. Otherwise it returns false and changes nothing.

Parcel ids, names and values are case sensitive.

  1. Example 1
    init
    []
    operations
    [["set",[1,"p1","color","red"]],["get",[2,"p1","color"]],["get",[3,"p1","size"]],["get",[3,"p2","color"]],["set",[4,"p1","color","blue"]],["update_if_match",[5,"p1","color","red","green"]],["get",[6,"p1","color"]],["update_if_match",[7,"p1","color","blue","green"]],["get",[8,"p1","color"]]]
    Output
    [null,"red",null,null,null,false,"blue",true,"green"]

    Why: set stores red, then overwrites it with blue. The update that expects red fails because the current value is blue, and the value stays blue. The update that expects blue succeeds, so the value is green. A name or parcel that was never set reads as null.

Constraints

  • 1 <= timestamp <= 10^9, and timestamp never decreases from one call to the next
  • 1 <= len(parcel_id), len(name) <= 32 and 0 <= len(value) <= 32, all ASCII
  • 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
Parcel Attribute Store: Airbnb interview question | LoopCoach