Bulls and Cows

MediumArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

In a guessing game, one player holds a secret digit string secret and the other submits a guess of the same length. The feedback reports two numbers: - **bulls**: positions where guess has exactly the same digit as secret. - **cows**: digits of guess that do appear in secret, but not at that position. Each secret digit can be matched at most once: bulls are counted first, and the remaining unmatched digits of each value pair up as cows. Return the feedback as the string "xAyB", where x is the number of bulls and y is the number of cows.

Examples

Input: secret = "1807", guess = "7810"
Output: "1A3B"
The 8 is a bull. 7, 1 and 0 are in the secret but in other positions, giving 3 cows.
Input: secret = "1123", guess = "0111"
Output: "1A1B"
The second 1 is a bull. Only one unmatched 1 is left in the secret, so just one of the guess's other 1s is a cow.

Constraints

  • 1 <= secret.length, guess.length <= 1000
  • secret.length == guess.length
  • Both strings consist of digits only.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems