Practice problem · Meta · Medium

In-Memory Database with TTL

An in-memory store holds keys. 3 stages in Python written by you, with an assistant that only answers. Stage 1 free.

Stage 1 of 3: Fields, conditional writes, listings

Background. An in-memory store holds keys. Each key holds named fields, and each field holds an integer value. Every call is told the time at which it is made.

Implement the class FieldStore in store.py, with the methods put, read, replace_if, remove_if and list_fields.

  • put(now, key, field, value) stores value in field of key, creating the key and the field as needed and replacing any value the field held.
  • read(now, key, field) returns the value in field of key, or None when that key or field holds no value.
  • replace_if(now, key, field, expected, value): if the field holds exactly expected, stores value in it and returns True. If the field holds no value or a different one, returns False and changes nothing.
  • remove_if(now, key, field, expected): if the field holds exactly expected, removes the field and returns True. If it holds no value or a different one, returns False and changes nothing.
  • list_fields(now, key, prefix="") returns the fields of key whose names start with prefix, as a list of (field, value) tuples sorted by field name in Python's string order. The default prefix="" lists every field; a key with no fields lists [].

now is the time of the call: an integer from 0 up to 10^15 that is larger on every call than on the one before. The answers here do not depend on it. Only put, a successful replace_if and a successful remove_if change the store.

Example

s = FieldStore()
s.put(1, "lamp", "watts", 40)
s.put(2, "lamp", "hours", 3)
s.put(3, "lamp", "hue", 7)
s.read(4, "lamp", "watts")                # 40
s.replace_if(5, "lamp", "watts", 60, 75)  # False: it holds 40
s.replace_if(6, "lamp", "watts", 40, 75)  # True
s.remove_if(7, "lamp", "hours", 3)        # True
s.list_fields(8, "lamp")                  # [("hue", 7), ("watts", 75)]
s.list_fields(9, "lamp", "h")             # [("hue", 7)]
s.read(10, "desk", "watts")               # None

Notes

  • Keys and fields are non-empty strings, prefix is any string, and values and expected are any integers. Inputs are always valid.
  • All 13 tests in tests/test_part1.py fail on the starter.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Values that run out. Values can now run out, and every call has to agree on when.
  2. Stage 3: Reading the past. Questions about what a field held at an earlier moment.

The files you get

  • Yours to edit: store.py.
  • Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.

The round: no AI, an assistant that only answers

Meta runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.

Frequently asked, briefly answered

Is In-Memory Database with TTL free?

Stage 1 is free with an account. The other 2 stages need Nyrion Pro.

Which company asks something like this?

Meta. It is modelled on Meta's coding round, which runs without AI. Based on candidates' reports of Meta interviews. Nyrion wrote this version, so the question you get may differ.

Do I bring my own AI tool?

No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.

What does the interviewer ask?

6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the first stage of this problem is free.

Start this problem → Sign in

More like it: every practice problem, Meta's interviews, the AI coding interview guide.