Crack a 4-digit code from exact-match counts
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding) and at Airbnb, last asked Feb 2026. It tests interactive, greedy and counting.
A server holds a secret 4-character code. Each character is a digit from '0' to '9'. Digits may repeat, and the code may start with zeros.
You can only learn about the code through one call:
class GuessServer:
def guess_number(self, guess: str) -> int: ...
guess_number takes any 4-digit string and returns how many positions hold the correct digit. It does not tell you about correct digits in the wrong position.
Implement crack(server: GuessServer) -> str, which returns the secret code. Use as few calls as you can, and state the worst-case number of calls your method needs.
Constraints
- The code length is 4 and the alphabet is
0-9. - The server is deterministic and the secret does not change.
- Brute force over all 10,000 codes is correct but too slow; aim for a small fixed bound.
Example 1
Secret "3071" (hidden from you).
-
guess_number("3000")returns2(the3in position 0 and the0in position 1 match). -
guess_number("1111")returns1(only position 3 matches). -
crack(server)returns"3071".
Example 2
Secret "0505".
-
guess_number("5555")returns2. -
guess_number("0000")returns2, so the code uses only the digits 0 and 5, twice each. -
crack(server)returns"0505".
Clarifying questions
- Is the cost only the number of calls, not my local computation?
- Can I guess codes with repeated digits, like
"0000"? - Should
crackverify its answer with a final call returning 4?
What a strong answer covers
- Finds how many times each digit appears with
"dddd"guesses: at most 9 calls, the tenth count follows from the total of 4. - Uses a digit that does not appear as filler (at least 6 digits are absent), then finds positions by probing one candidate digit in one position with filler elsewhere.
- Narrows each position to the digits still unplaced, deducing the last candidate without a call, and states the resulting fixed worst-case bound.
- Mentions a stronger approach that picks each guess to split the remaining candidates as evenly as possible (minimax over the 10,000 codes), with fewer calls in the worst case and more computation.
- Tests with a fake server on repeated digits (
"0505"), leading zeros, all one digit, and all distinct digits, counting calls.
Follow-ups
- The code has length
nover an alphabet ofmsymbols: give the call count in terms of both. - The server also reports correct digits in the wrong position (Mastermind).
- The server answers some calls wrongly.
Where it's asked
- Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
- Airbnb
What it tests
interactive · greedy · counting · puzzle
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

