Question presented to candidate: "We are adding a short status message box to our app, a bit like a post composer. Build a textarea that shows how many characters the user has typed out of a limit of 50, turns amber near the limit and red when they go over, and disables the Post button when the text is empty or too long. Then add an option that stops the user typing past the limit at all."
Requirements:
- A labelled textarea with a live counter such as
42 / 50 ยท 8 left, or3 overpast the limit. - Three visual states: normal, warning (10 or fewer left) and over the limit.
- A Post button that is disabled for empty, whitespace-only or over-limit text; posting clears the box.
- A "Block typing past the limit" option that truncates input to the limit.
- "Character" means what a person sees: an emoji with a skin tone or an accented letter counts as one.
What a strong answer should cover:
- One piece of state (the text); the count, remaining, status and button state are derived, not stored.
- Why
string.lengthis wrong for emoji (it counts UTF-16 code units) and howIntl.Segmenterwith grapheme granularity fixes it. - Why the native
maxlengthattribute is not a safe hard limit: it counts code units and can cut an emoji in half. - Soft limit (allow typing, show negative count) versus hard limit (truncate), and not truncating during IME composition.
- Accessibility:
aria-describedbylinking the counter,aria-invalidwhen over, and a polite live region that speaks only when the state changes.
Clarifying questions expected:
- "Is the limit about what users see, or about bytes or code units the backend stores?"
- "Should we block typing at the limit, or let people go over and fix it before posting?"
- "Do we need to support languages typed with an input method editor, such as Chinese or Japanese?"
Code / implementation expected: Yes. A working component with derived counter state, grapheme-aware counting and truncation, and a disabled submit, in the framework of your choice.