KatInterview

Build a Run-Once Typewriter Component for a Hidden Token

Data Structure DesignModel answer
MediumModel answerData Structure Designreacttypescripthooks

Last updated

Build a React and TypeScript component that finds a hidden token in the DOM or over HTTP, types it out every 50ms, never replays the animation, cleans up after itself, and has Mocha tests.

Task

You have a small React + TypeScript project that uses Mocha for tests. Build a component, plus any helpers it needs, that obtains a secret token (the "flag") and reveals it on screen one character at a time.

1. Obtaining the token

The component must support two sources.

DOM mode. Read the token from the current document. It can be in either of these places:

  • an element with a data-flag="..." attribute whose value is the whole token, or
  • an element with id="flag" whose text content is the token.

The element may be concealed (display:none, the hidden attribute, and similar). It must still be found.

HTTP mode. Send GET <flagUrl>. The response is one of:

  • JSON of the form { "flag": "FLAG{...}" }, or
  • plain text such as FLAG{...}.

2. Typewriter display

After the token is known, show it progressively.

  • Render into a container such as <div data-testid="flag-output">...</div>.
  • Add one character every 50 ms. The delay may be a prop so tests can control it.
  • When typing ends, the whole token stays on screen.

3. Run only once

If the component re-renders because of state or prop changes, the animation must not start over after it has finished.

4. Engineering constraints

  • Nothing may leak. Timers and in-flight requests must be cancelled when the component unmounts.
  • Code must tolerate both browser and Node test environments. For example, document might be undefined.

5. Tests

Write Mocha tests (or structure the code so an automated grader can verify it) covering:

  • DOM extraction for both the data-flag case and the #flag case.
  • HTTP parsing for both JSON and plain-text bodies.
  • The typewriter reveals characters over time and finishes.
  • The animation does not restart after a re-render.

Deliverables

  • A component (for example FlagViewer) and its helper functions.
  • Mocha tests that confirm the behavior above.

Explain your design choices and the trade-offs you made.

Hints

Hint 1

Separate the work into three layers: pure retrieval helpers that accept the document or fetch function as a parameter, a typing hook, and a thin component that connects them. Each layer can then be tested alone.

Hint 2

Hidden elements are still in the DOM tree. Think about which text-reading property depends on rendered layout and which one does not.

Hint 3

State alone is a weak place to record that the animation has finished. Consider what survives a re-render, or a StrictMode mount/cleanup/mount cycle, without triggering extra renders, and how an effect should behave when it re-runs.

Hint 4

For cleanup, pair every interval with a clearInterval, give each request an AbortController, and ignore the abort error caused by unmounting.