BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Data processing and features42 of 54 in topic

794. Build a key-value store you can read at any point in time

MediumData processing and features Pro

Config systems, feature stores and experiment trackers often need to answer 'what was this value at time t?' and 'what was the third version?'. Writes may arrive late, so timestamps are not always in order.

Write run_tkv(ops) that runs operations on an empty store and returns a list with one output per operation.

('set', key, value, t) records value for key at time t and returns the version number of this write for that key (1 for the first set of the key, 2 for the second, and so on, in call order). If the key already has an entry at exactly time t, the new value replaces it, but it still gets a new version number.

('delete', key, t) records that key has no value from time t on (until a later-timed set), and returns None. If the key already has an entry at exactly time t, the delete replaces it. Deletes do not get version numbers.

('get', key, t) returns the value of the entry with the largest time at most t, or None if there is no such entry or that entry is a delete.

('get_version', key, v) returns the value written by the v-th set of key, or None if there is no such version.

Functiondef run_tkv(ops)

Example 1

Input: [('set', 'lr', '0.1', 5), ('set', 'lr', '0.01', 10), ('get', 'lr', 7), ('get', 'lr', 4), ('get', 'lr', 99), ('get_version', 'lr', 1)]

Output: [1, 2, '0.1', None, '0.01', '0.1']

Example 2

Input: [('set', 'k', 'a', 10), ('set', 'k', 'b', 3), ('get', 'k', 5), ('delete', 'k', 7), ('get', 'k', 8), ('get', 'k', 12)]

Output: [1, 2, 'b', None, None, 'a']

Explanation: The second set arrived late but is placed at time 3.

Constraints

  • At most 20,000 operations.
  • Times are integers; versions start at 1.
Hint 1

bisect_right(times, t) - 1 is the index of the last time <= t.

Hint 2

Use a unique sentinel object for deletes.

Hint 3

Track versions in call order, separate from the timeline.

Common mistakes
  • Assuming timestamps arrive in increasing order and appending.
  • Returning the value before a delete instead of None.
  • Giving deletes a version number.
Understand the idea

A time-indexed store answers point-in-time reads with binary search over sorted timestamps. Tombstones represent deletes without losing history. Separating 'when it was valid' (event time) from 'which write it was' (version or arrival order) is the same idea behind point-in-time joins in feature stores and multi-version concurrency control in databases.

Topics
Data processing and features
Similar questions
  • Split a dataset so every class keeps its shareMedium
  • Bin a feature into quantile buckets learned from training dataMedium
  • Frequency-encode a category with a shared bucket for rare and unseen valuesMedium
  • Generate K-fold train and validation index splitsMedium
All Data processing and features problems
Report
794 / 903
Python 3solution.pyPython idle
1
2
3
4
def run_tkv(ops):
# return one output per operation
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS