AlgoMaster Logo
AlgoMasterApply Versioned Invalidation Eventsmedium

Apply Versioned Invalidation Events

medium

Invalidation messages may be duplicated, delayed, or delivered out of order. Version watermarks let a cache consume those events idempotently without allowing an old event to remove newer data or an old fill to resurrect invalidated data.

Design a VersionedInvalidationCache class:

  • boolean put(String key, int version) tries to cache a version.
  • boolean invalidate(String key, int eventVersion) processes an event and returns whether a cached entry was removed.
  • int getVersion(String key) returns the cached version, or -1 when absent.

Store the highest invalidation version observed per key, including absent keys. Ignore events at or below that watermark. A newer event removes the cached entry only when its version is at most the event version.

put rejects a version at or below the invalidation watermark or below the currently cached version. Equal-version puts are accepted as idempotent; newer safe versions replace the cache.

Example 1:
Example 2:

Constraints

  • 1 <= key.length <= 40
  • 0 <= version, eventVersion <= 10^9
  • At most 500 total method calls are made.
Hints

Loading...
CallReturns
new VersionedInvalidationCache()null
put("user", 5)true
getVersion("user")5
invalidate("user", 4)false
getVersion("user")5
invalidate("user", 5)true
getVersion("user")-1
put("user", 5)false

Event 4 advances the watermark but cannot remove cached version 5. Event 5 removes it, and a later attempt to cache version 5 is rejected by the watermark.

Run checks these cases. Submit also runs a larger hidden set.