← Questions

Crack a 4-digit code from exact-match counts

0
AirbnbGoogleCodingLast asked Feb 2026

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") returns 2 (the 3 in position 0 and the 0 in position 1 match).
  • guess_number("1111") returns 1 (only position 3 matches).
  • crack(server) returns "3071".

Example 2

Secret "0505".

  • guess_number("5555") returns 2.
  • guess_number("0000") returns 2, 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 crack verify 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 n over an alphabet of m symbols: 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

What it tests

interactive · greedy · counting · puzzle

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding