Reported September 2026
Instacartgraph

Evaluate Formulas with Cycle Detection

Reported by candidates from Instacart's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Instacart OA. Under 2s to a working solution.
Founder's read

Instacart's September 2026 OA hands you a list of spreadsheet-style formulas like "c = b - a + 2" and asks you to evaluate all of them, or return ["CYCLE"] if the dependencies loop. References can point forward, so you can't just scan top to bottom. It's a dependency graph problem wearing a parsing costume. Up to 100000 formulas means recursion depth is a real threat. If you blank on the ordering logic mid-assessment, StealthCoder runs invisibly on your screen and gives you a working solution as a safety net. Know the shape before you open the invite.

The problem

You are given an ordered array formulas. Each string is a space-separated assignment of the form name = operand or name = operand + operand - operand.... An operand is either a signed 64-bit integer literal or the name of another formula. Every name is defined exactly once, and references may point forward or backward in the array.
Evaluate every formula. Return one string name=value per formula in the original input order. If the dependency graph contains any cycle, return only ["CYCLE"].
Addition and subtraction are evaluated from left to right. All inputs follow the grammar below, every referenced name is defined, and every intermediate and final value fits in a signed 64-bit integer.

Function
evaluateFormulas(formulas: String[]) → String[]

Examples
Example 1
formulas = ["a = 5","b = a + 3","c = b - a + 2"]
return = ["a=5","b=8","c=5"]
a is 5, then b is 5 + 3 = 8, and c is 8 - 5 + 2 = 5. The result keeps assignment order.
Example 2
formulas = ["x = y + 1","y = z - 2","z = x + 4"]
return = ["CYCLE"]
The dependencies form x -> y -> z -> x, so no value in that component can be evaluated.

Constraints
1 <= formulas.length <= 100000.
Every formula contains an ASCII-letter name, a separated = token, and a valid alternating sequence of operands and separated + or - tokens; adjacent tokens have exactly one ASCII space.
Each formula name is unique, and every referenced name has exactly one formula.
The total number of space-separated tokens across all formulas is at most 500000.
Every integer literal, intermediate value, and result fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is topological sort. Parse each formula into a name, a list of operands, and signs. Each operand is either a literal or a reference. Build edges from each referenced name to the formula that uses it, count in-degrees, and run Kahn's algorithm with a queue. When a formula's dependencies are all resolved, compute its value left to right and push its dependents. If you process fewer nodes than formulas, there's a cycle, so return ["CYCLE"]. Then output name=value in the original input order. The pitfalls: recursive DFS can blow the stack at 100000 nodes, a formula can reference the same name twice (count in-degree per occurrence, or dedupe consistently), and negative literals like -5 need careful parsing against the separated - token. Use 64-bit ints. If the parsing or cycle logic goes sideways live, StealthCoder is your hedge. It reads the problem and hands you a clean Kahn's implementation.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Evaluate Formulas with Cycle Detection cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Instacart's OA.

Instacart reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Evaluate Formulas with Cycle Detection FAQ

What's the trick in Instacart's Evaluate Formulas problem?+

Treat formulas as nodes and references as edges, then run a topological sort. Evaluate a formula only once all its referenced names have values. If the sort can't process every node, a cycle exists and you return ["CYCLE"]. Parsing is the easy part. The ordering is the actual question.

Should I use DFS or Kahn's algorithm here?+

Kahn's with a queue. With up to 100000 formulas, a chain of dependencies can make recursive DFS hit the stack limit in many languages. Iterative Kahn's avoids that and detects cycles for free by comparing processed count against total formulas.

How do I handle duplicate references like a = b + b?+

Be consistent. Either increment in-degree once per occurrence and decrement once per occurrence when b resolves, or dedupe dependencies into a set first. Mixing the two approaches leaves in-degrees wrong and makes you report a false cycle.

How do I parse operands and signs correctly?+

Split on single spaces. Token 0 is the name, token 1 is =, then operands alternate with + or - tokens. An operand is a literal if it parses as an integer, possibly with a leading minus, otherwise it's a name. Store signs next to operands so evaluation runs left to right.

How do I prepare for this in 48 hours?+

Write Kahn's topological sort from memory twice, including the cycle check. Then write the parser separately and test on both examples plus a self-reference like a = a + 1 and a forward-reference chain. Keep output in original input order using a map from name to value.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Instacart.

OA at Instacart?
Invisible during screen share
Get it