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 attributenameof the parcel with the givenvalue, replacing any value it already had. It returnsnull.get(timestamp, parcel_id, name)returns the attribute's value, ornullwhen the parcel or the attribute does not exist.update_if_match(timestamp, parcel_id, name, expected, value)replaces the attribute's value withvalueand returnstrue, only when the attribute exists and its current value equalsexpected. Otherwise it returnsfalseand changes nothing.
Parcel ids, names and values are case sensitive.
- 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:
setstoresred, then overwrites it withblue. The update that expectsredfails because the current value isblue, and the value staysblue. The update that expectsbluesucceeds, so the value isgreen. A name or parcel that was never set reads asnull.
Constraints
1 <= timestamp <= 10^9, andtimestampnever decreases from one call to the next1 <= len(parcel_id), len(name) <= 32and0 <= len(value) <= 32, all ASCII- 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.