KatInterview

Letter-Match Feedback Widget for a Five-Letter Word Game

StringsModel answer
EasyModel answerStringsstringsfrontendstate-management

Last updated

Build a small browser widget that scores a typed guess of up to five letters against a fixed secret word, marking each letter exact, misplaced or absent, with sound input handling.

Task

Create a minimal browser component for a five-letter word-guessing game. The game holds one hidden word, and the player types a guess. Every letter of the guess gets one of three labels describing how it relates to the hidden word:

  • Green: the letter occurs in the hidden word at the same position.
  • Yellow: the letter occurs in the hidden word, but at another position.
  • Red: the letter does not occur in the hidden word.

For testing, use the hidden word SPEND. The player may type at most five letters. Visual polish is not evaluated; the three states must simply be easy to tell apart. Spend your effort on correct feedback and a smooth input flow.

Constraints

  • Time budget: about 45 minutes, including your own testing.
  • Hidden word: SPEND (five distinct uppercase letters).
  • Input: letters only, no more than five.
  • Colors are required; styling beyond that is not.

Questions worth settling up front

  • Is a framework mandatory, or is plain JavaScript fine?
  • Should colors update while typing or only after submission?
  • Must a guess be exactly five letters before it can be scored?
  • Must the guess be a real dictionary word?
  • Should earlier guesses stay visible, and is there an attempt limit?

State your assumptions explicitly in your answer.

Part 1: Scoring function

Write a pure function that takes the hidden word and a guess and returns one status per guessed letter. Example: against SPEND, the guess SNEAK yields green, yellow, green, red, red.

Decide and justify how to handle:

  • A guess containing a letter more times than the hidden word does (for example SPEED against SPEND). Which copies are colored, and which become red?
  • Case differences.
  • Guesses shorter than five letters, if you allow them.

Part 2: Input and rendering

Build the interface: an input restricted to at most five letters, and a row of tiles colored by the Part 1 statuses. Explain:

  • How you restrict input to letters, cap its length, normalize case, and cope with pasted text.
  • What you keep in state (current text, submitted guesses) and what you derive (statuses, whether the puzzle is solved).
  • How a guess is submitted (button, Enter key, or both) and how statuses map to colors.

Hints

Hint 1

Compute the scoring in two passes: first mark exact-position matches, then handle the remaining letters. Doing both in one left-to-right pass is where duplicate-letter bugs creep in.

Hint 2

Keep a count of the hidden word's letters that were not consumed by an exact match. A non-exact guessed letter is yellow only if its remaining count is above zero (and you decrement it); otherwise it is red.

Hint 3

Store only the raw current input and the list of submitted guesses. Statuses and the solved flag can be recomputed from those during render. Sanitize input in one place: uppercase it and strip anything that is not A-Z, then slice to five characters.