KatInterview

React Grid with Formula Dependencies and Virtual Rendering

Data Structure DesignModel answer
MediumModel answerData Structure Designreactvirtualizationmemoization

Last updated

Build a React component that displays a large editable grid, evaluates cell formulas with references, detects cycles, and renders only visible rows.

You need to create a React component that displays a spreadsheet-like grid where users can edit cells and enter formulas that reference other cells.

What You Need to Build

Create a Spreadsheet component that accepts these props:

interface SpreadsheetProps {
  rowCount: number;          // Total number of rows
  colCount: number;          // Number of columns (1-26 for A-Z)
  cellWidth: number;         // Width of each cell in pixels
  cellHeight: number;        // Height of each cell in pixels
  viewportWidth: number;     // Container width
  viewportHeight: number;    // Container height
  overscan?: number;         // Extra rows/cols to render beyond visible area (default: 3)
  initialData?: Record<string, string>; // Starting cell values like { "A1": "Hello" }
}

Core Features

1. Cell Editing and Display

  • Columns are labeled A through Z, rows are numbered starting from 1
  • Users can click a cell to edit its raw content
  • Display the evaluated result when not editing

2. Formula System

  • Any cell value starting with = is treated as a formula
  • Formulas can reference other cells (e.g., A1, B23) and concatenate strings with &
  • String literals are enclosed in double quotes: "text"
  • Example: =A1 & " " & B2 concatenates the values from A1 and B2 with a space between

3. Error Handling

  • If a formula references a non-existent cell, show #REF!
  • If circular dependencies exist (A1 depends on B1, B1 depends on A1), show #CYCLE! for all cells in the cycle

4. Performance Requirements

  • Virtual rendering: With up to 50,000 rows, you cannot render all cells as DOM elements. Only render cells currently visible in the viewport plus a small buffer (overscan)
  • Selective re-rendering: When a user edits cell A1, only A1 and cells that depend on A1 (directly or transitively) should re-evaluate. Independent cells should not re-render

Example Behavior

Starting state:

  • A1: "Hello"
  • B1: "World"
  • C1: "=A1 & \" \" & B1"
  • D1: "=C1 & \"!\""

Display shows:

  • A1: Hello
  • B1: World
  • C1: Hello World
  • D1: Hello World!

User changes A1 to "Hi":

  • Only A1, C1, and D1 recalculate
  • B1 and unrelated cells don't re-render
  • New display: C1 shows "Hi World", D1 shows "Hi World!"

Constraints

  • Maximum 50,000 rows
  • Maximum 26 columns (A-Z)
  • Formula syntax includes only: cell references, string literals in quotes, and & operator

Hints

Hint 1

Build a dependency graph tracking which cells reference which other cells—this enables targeted updates when a cell changes.

Hint 2

For virtualization, calculate which row and column indices are visible given the scroll position, then render only those plus overscan buffer.

Hint 3

Use React.memo or useMemo to prevent cell components from re-rendering unless their own data or dependencies change.