Question presented to candidate: "Build a small form with two number inputs, Rows and Columns, and a Generate button. When the user submits, render a table of that size filled with the numbers 1 to rows times columns. The numbers go down the first column, then up the second column, down the third, and so on, like a snake."
Requirements:
- Two labelled number inputs (1 to 20) and a Generate button; Enter in either input submits.
- The table only changes when the form is submitted, not while typing.
- Cell values snake through the columns: for 4 x 5 the first row is
1 8 9 16 17. - Invalid input (empty, 0, decimals, above 20) shows an error per field, marks it invalid, and keeps the last valid table on screen.
What a strong answer should cover:
- Separate draft state (what is in the inputs, as typed) from committed state (the last valid size); the grid is derived from the committed size.
- A closed-form formula for each cell: even column
col * rows + row + 1, odd columncol * rows + (rows - row). - Number input quirks: the DOM value is a string, empty inputs, decimals, and framework conversions (Vue
v-modeland AngularngModelhand back numbers). - Accessible errors with
aria-invalidandaria-describedby, a<caption>, and a formsubmithandler rather than a click handler. - Stable keys when rendering rows and cells, and an upper bound to keep the DOM small.
Clarifying questions expected:
- "Is the snake order column-wise or row-wise, and does it start at 1?"
- "What are the maximum sizes, and should the table update live as I type?"
Code / implementation expected: Yes. A form with validation and a generated table in the framework of your choice.