KatInterview

Design an NL-to-Formula Assistant for a No-Code Spreadsheet Tool

Machine LearningModel answer
HardModel answerMachine Learningllm-orchestrationschema-groundingstructured-output

Last updated

Design an ML system that converts natural-language requests into validated formula expressions grounded in a live table schema, with optional write-back to the data source.

Context

You are building a feature for a no-code spreadsheet product (think: a product where users define tables with typed columns and write formulas to compute derived values). Non-technical users want to describe what they want in plain English and have the system produce a correct, validated formula — and optionally apply it to their live table.

You have two API keys: one for the table product (read/write access to a base and table), and one for an LLM provider. The table product has its own formula language with typed functions (IF, AND, DATETIME_DIFF, etc.) and typed fields (number, currency, date, checkbox, single-select, text).

Example user requests:

  • "Add 2 to salary."
  • "If Status is 'Won' and Amount is over 1000, give me 10% of Amount, otherwise 0."
  • "Create a column that flags a row as 'Late' if Due Date passed today and Completed is unchecked."
  • "Extract the domain part of the Email field."
  • "Set bonus to 10% of salary only when Status is 'active'."

What to cover

Design the full system end to end. Walk through:

  1. Architecture. Components (client, orchestrator, LLM layer, schema service, validators, write executor, observability) and how data flows through them.

  2. Formula generation strategy. How you prompt the model, why a typed intermediate representation (IR) is better than asking the model to emit raw formula text, and how the IR gets compiled deterministically into a formula string.

  3. Output contract. What the system returns beyond the formula itself — explanation, assumptions, sample test cases — and how those are generated and verified.

  4. Schema grounding and type checking. How you pass the live schema to the model, prevent hallucinated field names or functions, and enforce type correctness.

  5. Ambiguity resolution. When and how to ask clarifying questions (e.g. two fields that could both match "salary", an underspecified date boundary), and how to carry state across a multi-turn refinement loop.

  6. Validation before any write. Both static (parse to AST, type-check, lint) and dynamic (sandbox evaluation on sampled rows) checks, and a bounded repair loop that feeds errors back to the model.

  7. Applying changes to the live table. Two execution modes — (a) creating/updating a formula-typed column and (b) materializing computed values into a regular column — with a preview diff and explicit user confirmation gating any destructive write.

  8. Safety. Prompt-injection defense, credential isolation (the LLM never sees API keys), PII redaction in samples, a capability whitelist, and confirmation gates for bulk or destructive operations.

  9. Latency and reliability. How to hit a P95 of ~2–3 s for the suggestion, and how to handle API rate limits, retries, and partial failures during bulk writes.

  10. Evaluation. Offline metrics (AST equivalence, execution accuracy on labeled test rows, validation pass rate) and online metrics (acceptance rate, edit distance, turns to success, post-apply undo rate).

  11. Key failure modes. Hallucinated fields/functions, operator precedence bugs, null/blank handling, type mismatches, locale quirks (percentages, currency), timezone edge cases, and schema changes between preview and apply.

Hints

Hint 1

Before describing components, pin down the hardest constraint: the table product's API may not support creating formula-typed fields, which makes materialization a first-class path, not a fallback. That shapes the whole write path.

Hint 2

Ask the model to emit a structured JSON AST (an IR), not a raw formula string. Constrained decoding over an enum of real field IDs and a whitelist of functions prevents hallucination by construction, and a deterministic compiler handles all quoting, bracing, and parenthesization.

Hint 3

The repair loop (feed structured validation errors back to the model) should be bounded at 2 iterations and should escalate to a larger model on the first failure. Returning a broken formula silently is worse than returning an 'unsupported' status with a clear reason.