Quick Overview

This question evaluates proficiency in React-based front-end development, including state and component management, user interaction handling, and algorithmic skills for string comparison and duplicate-letter matching.

Build a Wordle-style game in React

Company: Ramp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

## Task Implement a simplified **Wordle-style** game as a small React app. **No styling/CSS is required** (plain HTML output is fine). ## Game rules (assume Wordle-like defaults) - The secret word is a fixed **5-letter** English word (you may hardcode it, or pass it in as a prop). - The player has at most **6 attempts** to guess the word. - Each guess must be exactly **5 letters** (A–Z). Reject/ignore invalid submissions. - After each submitted guess, show per-letter feedback: - **Correct**: the letter is in the correct position. - **Present**: the letter exists in the secret word but in a different position. - **Absent**: the letter does not exist in the secret word. - Letter matching should handle duplicates correctly (e.g., if the secret has one `A`, only one `A` in the guess can be marked as Correct/Present). ## UI/Interaction requirements - Allow the user to type a guess (e.g., an input box) and submit (e.g., Enter key or a button). - Display the history of guesses, each with its feedback (text labels are fine, e.g., `C/P/A` per character). - End state: - If the user guesses the secret word, show a “You win” message and stop accepting further guesses. - If the user uses all attempts without guessing correctly, show a “Game over” message and reveal the secret word. ## Clarifications - You may assume a single round (no need for restart unless you want to add it). - No backend is required. - No external UI libraries required. ## Example Secret: `CRANE` Guess: `CANDY` Feedback (conceptually): `C=Correct`, `A=Present`, `N=Present`, `D=Absent`, `Y=Absent`

Overview: This question evaluates proficiency in React-based front-end development, including state and component management, user interaction handling, and algorithmic skills for string comparison and duplicate-letter matching.

Implement the core game engine behind a simplified Wordle-style React app. Write a function that simulates one round of the game. The function receives a secret 5-letter word and a list of submitted guesses in order. Process guesses until the player wins or 6 valid guesses have been accepted. Rules: - A valid guess is exactly 5 alphabetic characters. Invalid guesses are ignored and do not use an attempt. - Comparison is case-insensitive. - For each accepted guess, produce a 5-character feedback string using: - 'C' = correct letter in the correct position - 'P' = letter exists in the secret word but in a different position - 'A' = letter does not exist in the secret word - Duplicate letters must be handled correctly. A guessed letter can only be marked as Correct/Present as many times as it appears in the unmatched part of the secret. - Stop accepting guesses after a win or after 6 valid guesses. Return a dictionary with: - 'history': a list of (GUESS, FEEDBACK) tuples for each accepted guess, using uppercase guesses - 'status': 'win', 'game_over', or 'incomplete' - 'secret': the secret word only when status is 'game_over'; otherwise None

Constraints

  • secret is a 5-letter English word containing only alphabetic characters
  • 0 <= len(guesses) <= 10^4
  • Only guesses of length 5 containing letters A-Z count toward the 6-attempt limit
  • Processing stops immediately after a win or after 6 valid guesses

Examples

Input: ('CRANE', ['crane'])

Expected Output: {'history': [('CRANE', 'CCCCC')], 'status': 'win', 'secret': None}

Explanation: The first valid guess matches the secret exactly, so the player wins immediately.

Input: ('APPLE', ['ALLEY'])

Expected Output: {'history': [('ALLEY', 'CPAPA')], 'status': 'incomplete', 'secret': None}

Explanation: A is correct, the first L is present, the second L is absent because only one unmatched L remains in the secret, E is present, and Y is absent.

Hints

  1. Use a two-pass approach for each guess: first mark exact matches, then use remaining letter counts to mark present letters.
  2. Keep a separate counter for valid guesses, since invalid submissions should be ignored and must not consume an attempt.

Loading coding console...

Show the approach

Approach

The solution wraps a two-pass Wordle feedback evaluator in a game loop.

evaluate(secret, guess) scores one guess against the secret:

  • Pass 2 — present letters. Re-scan the non-'C' positions. If the guessed letter ch still has remaining[ch] > 0, mark it 'P' and decrement the count; otherwise it stays 'A'. Decrementing is what makes duplicates correct: a letter can be P only as many times as it appears in the unmatched part of the secret, and exact matches never feed the pool (so e.g. ALLEY vs APPLE → CPAPA).

Game loop. secret is uppercased once. For each raw guess it stops early if the game already ended or 6 valid attempts were used. It skips invalid guesses (not isinstance(str), length ≠ 5, or non-alphabetic) without spending an attempt, uppercases the rest, evaluates, and appends (GUESS, FEEDBACK). On an exact match it sets status='win'; on the 6th valid attempt without a win it sets status='game_over'. Otherwise the loop exhausts the list as 'incomplete'.

The result dict exposes secret only when status == 'game_over', matching the spec. Correctness rests on the two-pass split: counting unmatched secret letters first guarantees Present marks never over-count beyond available occurrences.

Time complexity:
O(m)
Space complexity:
O(1)