Practice problem · Lyft · Medium

Commit and Rollback

memdb.py holds MemDB, an in-memory store of string keys and string values. 3 stages in Python written by you, with an assistant that only answers. Pro.

Stage 1 of 3: Keys, values and tallies

memdb.py holds MemDB, an in-memory store of string keys and string values.

Implement MemDB.put(key, value), fetch(key), remove(key) and tally(value) in memdb.py.

  • put(key, value) makes key hold value, whether it held another value, the same one, or nothing. It returns None.
  • fetch(key) returns the value key holds, or None when it holds none.
  • remove(key) makes key hold no value and returns None. Removing a key that holds no value changes nothing and raises nothing.
  • tally(value) returns how many keys hold exactly value now: 0 when no key does, a value never stored included.

Example

db = MemDB()
db.put("rider:17", "idle")
db.put("rider:42", "idle")
db.put("driver:5", "on-trip")
db.tally("idle")       # 2
db.put("rider:17", "matched")
db.tally("idle")       # 1: rider:17 no longer holds "idle"
db.fetch("rider:17")   # "matched"
db.remove("rider:42")
db.fetch("rider:42")   # None
db.tally("idle")       # 0

Notes

  • Keys and values are non-empty strings of any characters, compared exactly: "Idle" and "idle" differ, as do "7" and "07", and a value may be the same string as some key. Inputs are always valid.
  • Several MemDB objects can be alive at once; each is its own store.
  • On the starter, all 12 tests in tests/test_part1.py fail.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: memdb.py
  • Read-only: all tests/test_*.py files
  • Keys and values are non-empty strings; inputs are always valid

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: Transactions inside transactions. Changes can now be grouped and undone, and a group can open inside another.
  2. Stage 3: Deep and wide. Tens of thousands of transactions open at once over a large store, and every call still quick and small.

The files you get

  • Yours to edit: memdb.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

Lyft 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 Commit and Rollback free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

Lyft. It is modelled on Lyft's coding round, which runs without AI. Based on candidates' reports of Lyft 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 Pro plan starts at the first stage of this problem.

Start this problem → Sign in

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