Home
← All questions

Interactive diagonal grid highlight | Frontend Machine Coding

MediumPremiumMachine Coding

Frontend Machine Coding interview practice: Render an N x N grid that highlights cells diagonally on hover/click.

Problem

Build an N x N grid of cells that responds interactively — e.g., hovering a cell highlights its diagonals (or selecting cells highlights along the diagonal of the clicked cell).

Requirements

  • Render an N x N grid (N configurable).
  • On hover/click of a cell (r, c), highlight all cells on the same diagonals: where r - c === R - C (main) and r + c === R + C (anti-diagonal).
  • Clear highlight on mouse leave (for hover variant).

Hints / Approach

  • Store cells as a flat array of { row, col } or compute from index: row = Math.floor(i / N), col = i % N.
  • A cell is on the active diagonal if row - col === activeRow - activeCol or row + col === activeRow + activeCol.
  • Keep only the active cell in state; derive highlight per render to avoid storing O(N^2) flags.
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium