Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement In-Memory DB With TTL

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Implement In-Memory DB With TTL. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In-Memory DB: Implement set, get, and delete methods with TTL functionality.

Implement run_commands(commands), which processes operations in order. Each set command provides a key, value, timestamp, and positive TTL. A key expires when current_time >= set_timestamp + ttl. get returns the value or None, while delete removes the key if present. Return the results of all get commands in order.

Input commands use ['set', key, value, timestamp, ttl], ['get', key, timestamp], and ['delete', key, timestamp]. Keys and values are strings, and timestamps are nonnegative integers.

Constraints

  • 1 <= commands.length <= 10^4
  • Each command is a valid set, get, or delete operation
  • Keys and values are nonempty strings
  • Set TTL values are positive integers
  • Timestamps and TTL values are nonnegative integers
  • A get command is the only command that contributes to the output

Function Signature

def run_commands(commands):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output