Skip to the solution
MediumMachine Coding12 min read

Build a Password Generator

916 views

The problem

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 that crypto.getRandomValues() is the browser tool for it.
  • Modulo bias: random % n favours some characters unless 2^32 (or 256) is a multiple of n; 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.

  • React
  • Forms
  • Logic

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

12 min read · 9 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import PasswordGenerator from "./src/components/PasswordGenerator";
import "./styles.css";

export default function App() {
  return (
    <main className="page">
      <PasswordGenerator />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 36 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a Selectable Cell Grid (drag to select)

Continue