Question presented to candidate: "Build a password generator: a length slider, checkboxes for uppercase, lowercase, digits and symbols, a strength indicator, and a copy button. Every password must contain at least one character from each type the user ticked. Tell me how you make it actually random."
Requirements:
- Length from 8 to 64 (default 16) and four character classes (all on by default). Changing any setting generates a new password; a button generates another.
- Every generated password contains at least one character from each selected class, and only characters from selected classes.
- With no class selected: no password, the generate and copy buttons disabled, and an explanation.
- A strength meter based on estimated entropy (bits), with a label (Weak, Fair, Strong, Very strong).
- Copy to clipboard with a "Copied" status that disappears after 2 seconds, and a fallback message when the clipboard refuses.
What a strong answer should cover:
- Why
Math.random()is wrong for secrets (not a cryptographically secure generator) and thatcrypto.getRandomValues()is the browser tool for it. - Modulo bias:
random % nfavours some characters unless 2^32 (or 256) is a multiple ofn; fix with rejection sampling. - The class guarantee: one character from each chosen class, the rest from the combined pool, then a Fisher–Yates shuffle so the guaranteed characters are not always first.
- Entropy estimate
length × log2(poolSize)and its limits; the meter is derived from the settings, not stored. - Clipboard API (
navigator.clipboard.writeText, async, needs a secure context and may be refused), a polite status message, and clearing timers on unmount.
Clarifying questions expected:
- "Should we exclude look-alike characters such as 0/O and 1/l?"
- "Which symbol set does the target system accept?"
- "Is the strength meter for generated passwords only, or also for passwords people type?"
Code / implementation expected: Yes. A generator using crypto.getRandomValues with unbiased sampling and a verified class guarantee, plus the UI with a strength meter and copy.