Order Calculated Columns And Detect Circular Formula References
Last updated: November 29, 2026
Quick Overview
Given a workbook table where each calculated column has a formula that references other columns by name in brackets, return a valid evaluation order, and when there is a cycle, report the columns that cannot be evaluated plus one concrete cycle path for the error message.
Sigma Computing
November 29, 20260
0
0 solved
Given a workbook table where each calculated column has a formula that references other columns by name in brackets, return a valid evaluation order, and when there is a cycle, report the columns that cannot be evaluated plus one concrete cycle path for the error message.
Sigma users define columns like `[Revenue] - [Cost]` that can reference other calculated columns, and the compiler must order them before generating SQL. Interviewers look for a correct topological sort, a helpful cycle diagnostic, and thought about unknown references.
What the Interviewer Expects
- Extract dependencies from formula text with a simple parser or regex
- Use Kahn's algorithm (or DFS) to produce an order where every column comes after its inputs
- Identify every column blocked by a cycle, including columns that only depend on one
- Return an actual cycle path so the UI can say A -> B -> C -> A
- Fail fast on references to columns that do not exist
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- When a user edits one formula, how do you recompute only the affected columns?
- How would you make the order deterministic so generated SQL is stable for caching?
- How would cross-table lookups (a column referencing another table element) change the graph?
- How would you compile the order into nested CTEs versus a single SELECT?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is {column_name: formula_or_None}; None means a raw warehouse column. Output is an evaluation order plus the sorted list of columns stuck behind a cycle. A second function returns one cycle ...
High-Level Design
Build a graph with an edge from each referenced column to the column that uses it. Kahn's algorithm repeatedly emits nodes with zero unresolved inputs. Anything never emitted either sits on a cycle or...