Beyond Big O

Expiring key-value store

Expiring key-value store

Medium

Your task

  • Build an in-memory key-value store with set, get and delete.
  • A write stores or replaces the value for a key.
  • A get returns its value or an explicit missing result.
  • A delete returns whether a key was present and removes it when present.
  • Empty strings are valid values.
  • Keep separate stores independent.

Required API

  • NewExpiringStore
    func NewExpiringStore() *ExpiringStore
  • ExpiringStore.Set
    func (s *ExpiringStore) Set(key, value string, ttl, now int64)
  • ExpiringStore.Get
    func (s *ExpiringStore) Get(key string, now int64) (string, bool)
  • ExpiringStore.Delete
    func (s *ExpiringStore) Delete(key string, now int64) bool

Constraints

  • For this part, every operation occurs before the stored value expires.
  • Keep the provided ttl and now arguments in your interface; expiry behavior will be introduced in the next part.
  • Keys are valid case-sensitive ASCII identifiers, 1–32 characters.
  • Values are ASCII strings, 0–64 characters.
  • At most 1,000 operations occur per store.
  • Use only the standard library.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · missing

Calls in order; values are shown as data.
CallReturns
NewExpiringStore()
Instance 1
instance1.Get("x", 0)
Missing (found = false)
instance1.Delete("x", 0)
false
Test data
Input
{"ops":[["get","x",0],["delete","x",0]]}
Expected output
[null,false]

Example 2 · empty and delete

Calls in order; values are shown as data.
CallReturns
NewExpiringStore()
Instance 1
instance1.Set("x", "", 10, 0)
No return value
instance1.Get("x", 0)
""
instance1.Delete("x", 1)
true
instance1.Get("x", 1)
Missing (found = false)
instance1.Delete("x", 1)
false
Test data
Input
{"ops":[["set","x","",10,0],["get","x",0],["delete","x",1],["get","x",1],["delete","x",1]]}
Expected output
[null,"",true,null,false]

Related guides

Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…