Mastermind Guess Score
Updated
Problem statement
A code is a row of four balls. Each ball has one of four colors, written as a letter: R (red), G (green), B (blue) or Y (yellow). One code is the hidden solution, and a player tries a guess code. Both are strings of four letters, and spot i of a code is its character at index i.
The game answers the guess with two counts:
- Locations: the number of spots where the guess has the same color as the solution.
- Colors: the number of other guess balls whose color is in the solution, but at a different spot.
Each ball is counted at most once. A ball counted as a location is not counted again as a color. After the locations are removed, each color adds to the colors count the smaller of two numbers: how many of its balls are left in the solution and how many are left in the guess.
Return [locations, colors]. A guess equal to the solution gives [4,0].
Function
masterMind(solution: String, guess: String) → int[]Examples
Example 1
solution = "BGRR"guess = "RGYY"return = [1,1]Spot 1 is G in both codes, so there is 1 location. The left-over solution balls are B, R, R and the left-over guess balls are R, Y, Y. Only red is in both: 2 red balls in the solution and 1 in the guess, so it adds 1 color. Yellow is not in the solution. The answer is [1,1].
Example 2
solution = "RGBY"guess = "RGBY"return = [4,0]The guess equals the solution, so all 4 spots are locations and no ball is left over for colors.
Example 3
solution = "RGBY"guess = "RRRR"return = [1,0]Spot 0 is red in both codes, so there is 1 location. The solution has no other red ball, so the other three red guess balls add no color. The answer is [1,0].
Example 4
solution = "RRGG"guess = "GGRR"return = [0,4]No spot matches. The two red balls of the guess match the two red balls of the solution, and the two green balls match the two green balls, so there are 4 colors.
Constraints
solution.length == 4guess.length == 4solutionandguessconsist only of the letters'R','G','B'and'Y'.
More Zillow interview questions
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Reverse StringOA · PHONE SCREEN · Seen Sep 2019
- Sort Objects by a FieldOA · Seen Aug 2018
- Convert a String to a LongOA · Seen Jan 2018
- Insert and Delete in a Trinary TreeOA · Seen May 2016
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Branch with the Most FilesCapital One · OACapital One interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- First Letter to Appear TwiceCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- First Non-Repeating CharacterAmazon · OA · ONSITE INTERVIEW
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions