Skip to the solution
EasyMachine Coding12 min read

Build a Typewriter Effect

781 views

The problem

Question presented to candidate: "Build the typewriter effect you see on landing pages: 'I build with React', where the last word types itself out letter by letter, pauses, deletes itself, then types the next word, looping forever. Make the speeds configurable, show a blinking caret, and make sure it is accessible."

Requirements:

  • A fixed prefix followed by an animated word from a list (React, Vue, Angular), looping.
  • Configurable timings: type speed per character, delete speed per character, hold time on the full word, gap before the next word.
  • A blinking caret after the text.
  • A Pause button (moving content that runs forever must be pausable).
  • Screen readers get the full sentence once, not a stream of partial words.
  • Exactly one timer at a time; nothing left running after unmount.

What a strong answer should cover:

  • The effect as a state machine: { index, count, deleting } with four phases (typing, holding, deleting, gap) and a pure plan(state) that returns the next state and the delay.
  • A single setTimeout per step, scheduled from the current state and cleared before every re-run, instead of nested timers or a setInterval with phase flags.
  • Why a missing cleanup breaks under React StrictMode (two loops, doubled speed) and how to prove it.
  • Props or inputs for timings, and keeping the word list referentially stable.
  • Accessibility: visually hidden full text, the animated part aria-hidden, a pause control (WCAG 2.2.2 Pause, Stop, Hide), caret animation off for reduced motion.

Clarifying questions expected:

  • "Should it loop forever or stop after the last word?"
  • "Should the cursor keep blinking while paused?"
  • "Can the word list change at runtime?"

Code / implementation expected: Yes. A reusable component with configurable timings, a single-timer state machine, a caret, a pause control and cleanup.

  • React
  • Timers
  • Animation

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 Typewriter from "./src/components/Typewriter";

// Defined outside the component so the array keeps one identity across renders.
const WORDS = ["React", "Vue", "Angular"];

export default function App() {
  return (
    <main className="page">
      <section className="card">
        <h1 className="title">Typewriter Effect</h1>
        <Typewriter
          prefix="I build with"
          words={WORDS}
          typeMs={100}
          deleteMs={50}
          holdMs={1000}
          gapMs={300}
        />
      </section>
    </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 15 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 nextEasy

Build a Read More / Show Less text component

Continue