Practice problem · Meta · Medium

Compiler Cost Estimator

A toy compiler back end turns straight-line three-address code (res1 = var1 + var2, one assignment per line, see data/) into machine code, and the team wants a cost estimate before compiling. 4 stages in Python with an AI assistant. Pro.

Stage 1 of 4: Infer the time cost

Background. A toy compiler back end turns straight-line three-address code (res1 = var1 + var2, one assignment per line, see data/) into machine code, and the team wants a cost estimate before compiling. costs.py has the parser and a stub for extract_time_and_mem_cost(path) -> (time, mem).

Nobody wrote the cost model down. The unit tests are the only specification: tests/test_part1_time.py asserts the time cost of six instruction files.

Implement the time cost in extract_time_and_mem_cost. Work out the rules from the assertions and the files they point at. The memory number is not checked yet; return any integer for it.

Done when Submit goes green.

Constraints

  • Python 3.12+ standard library only
  • Time limit: 5 seconds per test
  • Editable: costs.py
  • Read-only: data/*.txt, the public tests under tests/

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: Infer the memory cost. The memory cost rule, where the files are built to rule out the obvious guesses.
  2. Stage 3: Expressions and constants. Lines with several operators and constants: the same rules, more cases.
  3. Stage 4: Cost after the optimizer. The cost after the optimizer runs, defined only by how the numbers change.

The files you get

  • Yours to edit: costs.py.
  • Read-only: tests/__init__.py, data/instruction1.txt, data/instruction2.txt, data/instruction3.txt, data/chain.txt, data/fanin.txt, data/mixed.txt, data/wide.txt, data/expr1.txt, data/expr2.txt, data/expr3.txt, data/expr4.txt, data/deadend.txt, data/expr6.txt, data/expr5.txt, data/reuse.txt, data/expr7.txt, data/copy.txt, data/opt1.txt, data/opt2.txt, data/opt3.txt, data/opt4.txt, data/opt5.txt.
  • Tests you can read and run: tests/test_part1_time.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 Compiler Cost Estimator 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?

Meta. It is modelled on Meta's round: CoderPad with integrated AI assistant (GPT-4o mini, Claude Sonnet/Haiku, Gemini 2.5 Pro, Llama 4).

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?

4 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 Meta runs its AI round, the AI coding interview guide.