Requirements
- The game involves two roles: a codemaker who picks a secret number and a codebreaker who tries to guess it.
- After each guess, provide feedback:
- Bulls – digits that match both value and position.
- Cows – digits that match value but are in a different position.
- The program should read guesses from standard input and keep asking for guesses until the secret is fully revealed (all Bulls).
- Include unit tests for the scoring logic.
Examples
Example 1
Secret: "1234"
Guess: "1243"
Output: Bulls = 2, Cows = 2
Explanation: '1' and '2' are in the correct positions (bulls). '3' and '4' are present but in swapped positions (cows).
Example 2
Secret: "1111"
Guess: "1111"
Output: Bulls = 4, Cows = 0
Explanation: All digits match exactly.
Example 3
Secret: "1234"
Guess: "5678"
Output: Bulls = 0, Cows = 0
Explanation: No digits match.
Constraints
- The secret and guess are strings of digits of the same length (typically 4 digits).
- Digits are 0-9.
Notes
- You may use AI tools, but the task is straightforward; many candidates write most of the code manually and only consult AI for syntax reminders. The interviewer will evaluate how you organize the code and your testing approach.
- Pay special attention to duplicate digits: each digit in the secret should be counted at most once when tallying cows, to avoid overcounting when the guess contains repeated digits.
Preparation
- Design the scoring function using a frequency map (hashmap) of the secret's digits. First, count bulls in one pass while decrementing the frequency map for exact matches. Then, in a second pass, count cows by checking if the guess digit exists in the remaining frequency map, decrementing as you go to handle duplicates correctly.
- Build a game loop that reads from stdin and terminates when the guess matches the secret. Write unit tests covering: all bulls, all cows, mixed bulls and cows, and scenarios with duplicate digits.