Mastermind Solver
Updated
Problem statement
Mastermind is a code-breaking game. The secret code is a row of pegs, and each peg has one of colors colors, numbered 1 to colors. Colors can repeat. A code is written as a string of digits, such as "3632".
A guess is a code of the same length. The answer to a guess is two numbers:
black: the number of positions where the guess and the secret have the same color.white: the number of right colors in the wrong position. For each color, take the smaller of how many times it appears in the guess and in the secret. Add these over all colors, then subtractblack.
Your solver plays with this fixed strategy:
- A code is still possible if, for every earlier guess, it would have given the same answer as the secret did.
- Each guess is the smallest code that is still possible, comparing codes as strings. So the first guess is all
1s. - The game ends right after a guess gets
blackequal to the code length.
In the real game, another player keeps the secret hidden and only gives answers. Here, you are given secret so you can work out each answer yourself.
Return every guess in the order it is made. The last guess is always secret.
Function
solveMastermind(secret: String, colors: int) → List<String>Examples
Example 1
secret = "23"colors = 3return = ["11","22","23"]The first guess is "11". The secret has no 1, so the feedback is 0 black and 0 white. The codes that would give the same feedback are the ones with no 1: "22", "23", "32" and "33".
The next guess is the smallest of them, "22". Its first peg matches, so the feedback is 1 black and 0 white. Of the codes left, "23" and "32" would also give 1 black and 0 white against "22", while "33" would give 0 black. The next guess, "23", gets 2 black and ends the game.
Example 2
secret = "321"colors = 3return = ["111","122","312","321"]Guess "111" gets 1 black and 0 white: the secret has exactly one 1. The smallest code that agrees is "122".
Guess "122" gets 1 black (the middle 2) and 1 white (the 1 is in the wrong place). Only "312" and "321" agree with both answers.
Guess "312" gets 1 black and 2 white: all three colors are right, but only the 3 is in place. That rules out everything but "321", which is the last guess.
Example 3
secret = "3632"colors = 6return = ["1111","2222","2333","3234","3253","3632"]The feedback for the guesses, as black and white, is: "1111" gets 0 and 0, "2222" gets 1 and 0, "2333" gets 1 and 2, "3234" gets 2 and 1, and "3253" gets 1 and 2.
For example, against "2333" the secret shares one 2 and two 3s, three colors in total. One of them, the 3 in the third position, is in place, so the answer is 1 black and 3 - 1 = 2 white. After the fifth guess, "3632" is the only code that agrees with every answer, so it is the sixth guess.
Constraints
1 <= secret.length <= 51 <= colors <= 6- Each character of
secretis a digit from1tocolors.
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- String Interpolation with a Missing-Value ModeMongoDB · PHONE SCREENMongoDB interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Fixed-Order Task Run Time With CooldownWaymo · PHONE SCREENWaymo interview questions
- Character Counts with Retried Remote WorkersCoalition · PHONE SCREEN
- Risk-Priority Detection StoreCrowdstrike · PHONE SCREENCrowdstrike interview questions
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)