Stage 1 of 4: Text runs
Background. Run-length encoding writes a run of one repeated character as the run's length and the character, so a long run takes a few characters instead of many.
Implement compress_text(text) and decompress_text(code) in codec.py.
compress_text(text)returns the code oftext: for each maximal run of one character, in order, the run's length in decimal (ASCII digits, no leading zero), then the character. A run of one is written with its1, and a count has as many digits as it needs.compress_text("")is"".textholds ASCII characters (code points 0 to 127), any of them except the ten digits0to9: letters, punctuation, the space, the newline and the other control characters. A text that holds a digit raisesValueError, because a count followed by a digit would read as one longer count.decompress_text(code)returns the text thatcodestands for. A code is a sequence of pieces, each one or more digits (a count of at least 1) followed by exactly one character that is not a digit; a piece stands for its character repeated count times.decompress_text("")is"". Two pieces with the same character in a row are read one after the other:"2a3a"gives"aaaaa".decompress_textraisesValueErrorwhen a character has no count before it ("a","3ab"), when the code ends in digits ("7","3a4"), or when a count is 0 ("0a").
Example
compress_text("zzzz--q") # "4z2-1q"
decompress_text("12x1\n") # "x" * 12 + "\n"
Notes
- Texts in the tests hold at most 50,000 characters. Codes never have a count with a leading zero and never stand for more than 50,000 characters.
- On the starter, all 8 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
codec.py - Read-only: all
tests/test_*.pyfiles
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.
- Stage 2: Bit packing. Pack small integers into exactly as many bits as their width, and get them back exactly.
- Stage 3: Runs and packed blocks. A column of 32-bit integers, negatives included: long runs stay runs, everything else is packed tight.
- Stage 4: One value at a time. Values arrive one at a time and bytes arrive in pieces; neither side may wait for the whole column.
The files you get
- Yours to edit: codec.py.
- Tests you can read and run: tests/test_part1.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 RLE and Bit Packing 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?
Databricks. Based on candidates' reports of Databricks interviews. Nyrion wrote this version, so the question you get may differ.
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, or an answer-only assistant that can't write code.
What does the interviewer ask?
8 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.
More like it: every practice problem, how Databricks runs its AI round, the AI coding interview guide.