Practice problem · Instacart · Medium

Calculate Equation Expression

You get a list of equations, each giving a name a value, and the name whose value is wanted. 4 stages in Python written by you, with an assistant that only answers. Stage 1 free.

Stage 1 of 4: Names and numbers

You get a list of equations, each giving a name a value, and the name whose value is wanted.

Implement value_of(equations, target) in equations.py. It returns the value of the name target as an int.

  • equations is a list of strings name = rhs: a name, one space, =, one space, and a right-hand side. What follows = is a number, or a name whose value it takes.
  • A name is a letter followed by any letters and digits, and case matters: a and A are different names. A number is a non-negative integer below 10^9, written with no sign and no leading zero.
  • The equations come in any order, and there are at most 100 of them.
  • Every input is valid: every name used has exactly one equation, target included, and following the names from any name always ends at a number.

Example

equations = ["speed = limit", "limit = cap", "cap = 120", "lanes = 3"]
value_of(equations, "speed")   # -> 120
value_of(equations, "lanes")   # -> 3

All 8 tests in tests/test_part1.py fail until value_of is written.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: equations.py
  • Read-only: all tests/test_*.py files
  • At most 100 names; each equation is written name = rhs with one space on each side of =

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: Adding and subtracting. Right-hand sides add and subtract, and names feed many others.
  2. Stage 3: Cycles. Some names lead back to themselves.
  3. Stage 4: Missing and repeated names. Some names are never defined, and some are defined more than once.

The files you get

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

Instacart 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 Calculate Equation Expression free?

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

Which company asks something like this?

Instacart. It is modelled on Instacart's coding round, which runs without AI. Based on candidates' reports of Instacart 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?

5 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, Instacart's interviews, the AI coding interview guide.