PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-processing skills, array manipulation, and handling of duplicate character counts to produce per-position feedback. It is commonly asked to assess implementation of matching logic and edge-case management in Coding & Algorithms (string/array algorithms), and is primarily a practical application problem that also requires conceptual understanding of counting and match ordering.

  • easy
  • Tesla
  • Coding & Algorithms
  • Software Engineer

Generate Per-Position Guess Feedback

Company: Tesla

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Take-home Project

You are building a word-guessing feedback function. Given two strings, `target` and `guess`, of the same length, return an integer array `feedback` where each element describes how the character in `guess` compares to `target`: - `2`: the character is exactly correct, meaning `guess[i] == target[i]`. - `1`: the character is not in the correct position, but it appears somewhere else in `target` and has not already been consumed by another match. - `0`: the character does not match any remaining available character in `target`. Duplicate characters must be handled carefully. Exact matches should be counted first. Then, for the remaining unmatched positions, assign partial matches using the remaining character counts from `target`. Example: ```text target = "sabby" guess = "assby" output = [1, 1, 0, 2, 2] ``` Explanation: - Positions 3 and 4 are exact matches: `b` and `y`, so they receive `2`. - Among the remaining target characters, there is one `s`, one `a`, and one `b`. - `guess[0] = 'a'` exists in the remaining target characters, so it receives `1`. - `guess[1] = 's'` exists in the remaining target characters, so it receives `1`. - `guess[2] = 's'` has no remaining unmatched `s`, so it receives `0`. Implement a function that returns this feedback array.

Quick Answer: This question evaluates string-processing skills, array manipulation, and handling of duplicate character counts to produce per-position feedback. It is commonly asked to assess implementation of matching logic and edge-case management in Coding & Algorithms (string/array algorithms), and is primarily a practical application problem that also requires conceptual understanding of counting and match ordering.

You are building feedback for a word-guessing game. Given two strings, `target` and `guess`, of the same length, return an integer array `feedback` where `feedback[i]` describes how `guess[i]` compares to `target`: - `2` if `guess[i] == target[i]`. - `1` if `guess[i]` is not correct at position `i`, but the same character appears in some other unmatched position of `target`. - `0` if `guess[i]` does not match any remaining unmatched character in `target`. Important: handle duplicate characters correctly. Count all exact matches first. Then use the remaining unmatched characters in `target` to assign partial matches. Each character in `target` can be used at most once.

Constraints

  • 0 <= len(target) == len(guess) <= 200000
  • Both strings contain only lowercase English letters

Examples

Input: ("sabby", "assby")

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

Explanation: Positions 3 and 4 are exact matches. Among the remaining target characters, one 'a', one 's', and one 'b' are available. The first two guess characters consume 'a' and 's', but the third 's' has no remaining match.

Input: ("apple", "apple")

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

Explanation: Every character matches in the correct position.

Input: ("abbc", "bbba")

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

Explanation: Positions 1 and 2 are exact matches. After that, only 'a' and 'c' remain unmatched in the target, so the first 'b' gets 0 and the final 'a' gets 1.

Input: ("aab", "baa")

Expected Output: [1, 2, 1]

Explanation: The middle 'a' is an exact match. The remaining unmatched target characters are one 'a' and one 'b', so the first and last guess characters both get partial matches.

Input: ("aabc", "ddaa")

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

Explanation: There are no exact matches. Only two 'a' characters are available for partial matching, so the last two positions get 1 and both 'd' characters get 0.

Input: ("", "")

Expected Output: []

Explanation: Empty strings produce an empty feedback array.

Hints

  1. Mark exact matches first. If you try to count partial matches before exact ones, duplicate characters can be overcounted.
  2. Store the counts of unmatched characters from `target` in a frequency array or hash map, then scan the unmatched positions in `guess`.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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

  • Write SQL Data Transformation Queries - Tesla (medium)
  • Implement a Rollback Key-Value Store - Tesla (hard)
  • Compute suffix sums over waypoints - Tesla (hard)
  • Compute time to burn tree - Tesla (medium)
  • Implement a Timed Task Scheduler - Tesla (medium)