Practice problem · Rippling · Medium

Logger: Handlers and Search

A service logs one message at a time, and what happens to each message is configured as a list of handlers: one prints a shortened copy, one prints it in capitals, one keeps it for later. 3 stages in Python with an AI assistant. Pro.

Stage 1 of 3: Handlers

Background. A service logs one message at a time, and what happens to each message is configured as a list of handlers: one prints a shortened copy, one prints it in capitals, one keeps it for later. New kinds of handler get added over time, so adding one should be a small change.

Implement Logger and the four handlers in logger.py.

  • Logger(handlers) takes a list of handlers. log(message) gives the message to every handler, in list order, and returns the lines they emit, in that order.
  • Every handler is given the original message. A handler never sees what another handler produced.
  • A handler is any object with handle(message), which returns the line to emit, or None to emit nothing.
  • RemoveStringHandler(target) emits the message with every occurrence of target removed: one pass from left to right, each occurrence found after the end of the previous one, as str.replace does. target is a plain string, not a pattern.
  • TruncateHandler(max_chars) emits the first max_chars characters; a shorter message is emitted whole.
  • UppercaseHandler() emits the message with every letter in upper case.
  • StoreHandler() emits nothing and keeps the message. stored() returns the kept messages, oldest first.

Example

store = StoreHandler()
logger = Logger([RemoveStringHandler("debug "), TruncateHandler(5), UppercaseHandler(), store])
logger.log("debug cache miss")   # ["cache miss", "debug", "DEBUG CACHE MISS"]
store.stored()                   # ["debug cache miss"]

Done when Submit goes green.

Constraints

  • Python 3.12+ standard library only
  • Time limit: 10 seconds per test file
  • Editable: logger.py
  • Read-only: all tests/test_*.py files

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: Search. Find stored messages by keyword: whole words, in order, each message once.
  2. Stage 3: Punctuation at scale. Words arrive with punctuation glued on, and searches outnumber writes by a lot.

The files you get

  • Yours to edit: logger.py.
  • Read-only: tests/__init__.py.
  • Tests you can read and run: tests/test_part1_handlers.py. Hidden tests run on Submit.

The round: an agent in the editor

The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.

Frequently asked, briefly answered

Is Logger: Handlers and Search 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?

Rippling.

Do I bring my own AI tool?

No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, an answer-only assistant that can't write code, or no assistant at all.

What does the interviewer ask?

2 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, how Rippling runs its AI round, the AI coding interview guide.