Snowflake questions

Transactional Key-Value Store

SnowflakePhone screenMedium

Implement an in-memory key-value store as a class TxStore. Keys are lowercase strings and values are integers. Each test creates one TxStore and calls its methods in order; the result records every call's return value, including None. You build the store in stages: each part adds behavior on top of the previous parts, and all earlier behavior must keep working.

Part 1: Basic operations

Implement three methods on TxStore:

  • put(key, value) stores value under key, replacing any previous value. Returns None.
  • get(key) returns the value stored under key, or None if no value is stored.
  • delete(key) removes key and returns True if it was present, otherwise returns False.
  1. Example 1
    init
    []
    operations
    [["put",["a",5]],["get",["a"]],["get",["b"]],["delete",["a"]],["get",["a"]],["delete",["a"]]]
    Output
    [null,5,null,true,null,false]

    Why: put(a,5) stores 5; get(a) returns 5; get(b) finds nothing; delete(a) removes the key and returns True; get(a) is then None, and the second delete(a) finds nothing and returns False.

Constraints

  • 1 <= len(operations) <= 10^4
  • 1 <= len(key) <= 10
  • 'a' <= key[i] <= 'z'
  • -10^9 <= value <= 10^9