PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string processing, frequency counting, and edge-case handling by requiring implementation of Wordle-like feedback scoring for guess versus secret words.

  • hard
  • Verkada
  • Coding & Algorithms
  • Software Engineer

Implement WordGuess feedback scoring

Company: Verkada

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## WordGuess (Wordle-like) scoring Implement a function that scores a `guess` against a `secret` word of the same length. ### Scoring rules (per character) Return an integer array `result` of length `n` (where `n = secret.length = guess.length`): - **GREEN = 2**: `guess[i] == secret[i]` (exact match) - **YELLOW = 1**: `guess[i] != secret[i]`, but `guess[i]` occurs **elsewhere** in `secret` **and there is still unmatched availability** of that character after accounting for GREEN matches and other YELLOW assignments (i.e., respect letter multiplicity). - **GRAY = 0**: the character does not occur in `secret` (or all occurrences are already consumed by GREEN/YELLOW matches). This is the same matching behavior used by Wordle: GREEN matches are applied first; then YELLOWs are assigned based on remaining character counts in the secret. ### Input validation Throw an exception (or return an error) if any of the following are true: - `secret` or `guess` is null/empty - lengths differ - inputs contain invalid characters (define and enforce a rule, e.g., only lowercase English letters `a-z`) ### Examples 1. `secret = "hello"`, `guess = "abcde"` → `result = [0,0,0,0,1]` (`e` exists in `secret` but not at the same position) 2. `secret = "hello"`, `guess = "eabcd"` → `result = [1,0,0,0,0]` 3. Duplicate-letter behavior: `secret = "aabb"`, `guess = "bbba"` → `result = [1,2,0,1]` Explanation: index1 is GREEN (`b`), remaining secret letters have one `b` and two `a` available, so only one of the remaining `b`s can be YELLOW. ### What to implement Write the core scoring function (you do **not** need to build a full interactive game loop unless explicitly asked). - **Input:** `secret: string`, `guess: string` - **Output:** `int[] result` (values in `{0,1,2}`)

Quick Answer: This question evaluates proficiency in string processing, frequency counting, and edge-case handling by requiring implementation of Wordle-like feedback scoring for guess versus secret words.

Implement a function solution(secret, guess) that returns WordGuess/Wordle-style feedback for each position. Use 2 for GREEN (exact match), 1 for YELLOW (the guessed letter exists elsewhere in secret and still has unused availability after all GREEN matches are removed), and 0 for GRAY otherwise. Matching must respect duplicate letters, so all GREEN matches are processed first and YELLOW matches consume only the remaining available letters. Raise ValueError if secret or guess is not a string, is empty, lengths differ, or any character is outside 'a' to 'z'.

Constraints

  • For valid inputs, 1 <= len(secret) == len(guess) <= 100000
  • Inputs may contain only lowercase English letters 'a' to 'z'; otherwise raise ValueError

Examples

Input: ('hello', 'abcde')

Expected Output: [0, 0, 0, 0, 1]

Explanation: There are no exact matches. Only the final 'e' exists in secret with unused availability, so only index 4 is YELLOW.

Input: ('hello', 'eabcd')

Expected Output: [1, 0, 0, 0, 0]

Explanation: There are no exact matches. The first letter 'e' exists in secret, so index 0 is YELLOW and the rest are GRAY.

Input: ('aabb', 'bbba')

Expected Output: [1, 0, 2, 1]

Explanation: Index 2 is GREEN. After removing that match, the remaining secret letters are 'a', 'a', and 'b', so only guess[0]='b' and guess[3]='a' can be YELLOW.

Input: ('apple', 'allee')

Expected Output: [2, 1, 0, 0, 2]

Explanation: Indices 0 and 4 are GREEN. Among the remaining secret letters, only one 'l' is available, so the second 'l' and the middle 'e' are GRAY.

Input: ('crate', 'trace')

Expected Output: [1, 2, 2, 1, 2]

Explanation: Indices 1, 2, and 4 are GREEN. The remaining secret letters are 'c' and 't', so guess[0]='t' and guess[3]='c' are YELLOW.

Input: ('z', 'z')

Expected Output: [2]

Explanation: Single-character exact match.

Solution

def solution(secret, guess):
    if not isinstance(secret, str) or not isinstance(guess, str):
        raise ValueError('secret and guess must be strings')
    if len(secret) == 0 or len(guess) == 0:
        raise ValueError('secret and guess must be non-empty')
    if len(secret) != len(guess):
        raise ValueError('secret and guess must have the same length')

    for ch in secret:
        if ch < 'a' or ch > 'z':
            raise ValueError('secret contains invalid characters')
    for ch in guess:
        if ch < 'a' or ch > 'z':
            raise ValueError('guess contains invalid characters')

    n = len(secret)
    result = [0] * n
    remaining = [0] * 26

    for i in range(n):
        if guess[i] == secret[i]:
            result[i] = 2
        else:
            remaining[ord(secret[i]) - ord('a')] += 1

    for i in range(n):
        if result[i] == 0:
            idx = ord(guess[i]) - ord('a')
            if remaining[idx] > 0:
                result[i] = 1
                remaining[idx] -= 1

    return result

Time complexity: O(n). Space complexity: O(1) auxiliary space.

Hints

  1. Process the strings in two passes: mark exact matches first, then handle partial matches.
  2. Keep counts only for secret characters that were not matched as GREEN; a fixed-size array of length 26 is enough.
Last updated: Apr 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Merge Sorted Arrays In Place - Verkada (medium)
  • Find and Merge Camera Alert Intervals - Verkada (hard)
  • Find user who can access every camera - Verkada (medium)
  • Implement LRU and LFU caches - Verkada (medium)
  • Validate a 9×9 grid under constraints - Verkada (medium)