Here is the interview question prompt, presented for reference.
You're playing a game with a friend called Target and Vicinity. Target and Vicinity consists of one person thinking up a number in their head, and the other guessing that number.
Sounds simple enough-- the caveat is that we also want to credit close guesses. To estimate the proximity of the guess to the actual number, we use the concept of "targets" and "vicinities" in comparing accuracy.
It works like this: target
s are digits in the guessed number that have the same value of the digit in actual
at the same position. Here's an example of two target
s:
Actual: "34"
Guess: "34"
"2T0V"
Notice that both the actual number and the guess have the same values, in the same position, with regards to 3
and 4
.
Now onto "vicinities". Vicinities are digits in guess
that have the same value as some digit in actual
, but don't share the same position (hence the nomenclature).
Person 1 has to tell person 2 how many targets and vicinities there are by providing a string in this format:
`${number of targets}T${number of vicinities}V"
Here are some examples of inputs and ouputs:
const actual = "345"
const guess = "135"
// "1T1V"
// Because `5` is a T, and `3` is a V
const actual = "45624"
const guess = "24325"
// "1T2V"
// Because `2` is a T, and `4` and `5` are Vs
You're given two strings, an actual
and a guess
, as parameters. Write a function to return the number of targets and vicinities in the above string format. You may assume the strings are of the same length.
100000
0
- 9
O(n)
O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Targets and Vicinities.