Interview Questions

A solution consists of four balls from a set of four different colors......

Microsoft Interview Questions and Answers


(Continued from previous question...)

14. A solution consists of four balls from a set of four different colors......

Question:
A solution consists of four balls from a set of four different colors. The user tries to guess the solution.

If they guess the right color for the right spot, record it as being in the correct ‘Location’. If it’s the right color, but the wrong spot, record it as a correct ‘Color’. For example: if the solution is ‘BGRR’ and the user guesses ‘RGYY’ they have 1 ‘Location’ and 1 ‘Color’. A correct solution would be 4 ‘Location’ and 0 ‘Color’.

Write a program to, given a solution and a guess, calculate the response that we present to the user.

maybe an answer:


answer1:
Actually the best thing is to sort the array in ascending order (u could use merge / heap sort). Now this takes care of umpteen number of colours also. So if your array has 4 different colours

RBBGYRRBBGGYYRR
after sorting would look like
BBBBGGGRRRRRYYY

this should solve the issue

answer2:
/*my answer is O(n), where n is the size of the guess*/
string (string& sol, string& guess) {
int color=0, location=0;
int used[4];
used[0] = used[1] = used[2] = used[3] = 0;
for (int i = 0; i


answer3:
let us consider 4 location to fill [1],[2],[3],[4].
and four boxes containing balls of color blue[b],yellow[y],red[r],&green[g].
now [1] location can be filled by any type of balls.so it can be filled by any of 4 color balls.
now [2] location can be filled by any type of balls.so it can be filled by any of 4 color balls.
now [3] location can be filled by any type of balls.so it can be filled by any of 4 color balls.
now [4] location can be filled by any type of balls.so it can be filled by any of 4 color balls.
now the total of arrangements is equal to =4*4*4*4 =256
but at one time only one arrangement is possible with location.
so a chance that one will guess a right solution is =1\256.

a chance that one will guess wrong solution is 255\256.
hence our response to the user for a solution is 1\256
and for wrong guess is 255/256

answer2:
We can use 2 Lists one to hold elements of solution and one to hold elements of guess. We need 2 counters locCnt which counts number of Locations and clrCnt which counts number of colors. Now take each color in guess list and find the index of this color in Solution list if index of this color in solution list matches index of color in guess list matching location is found, increment location count and replace element at this location in solution list with a dummy value so that it is not used again, if color element is not found in solution at all continue with next element in guess list, if color element is found in solution list at an index different than that of the guess list check if guess list has same color at the index returned from solution list if so just continue as this will be accounted for later, if not increment the color count by one and replace the element in solution list with a dummy value so that it is not used again..

answer3:
Lets say solution is ‘RGTS’
and the guess is ‘PSTO’

Put these two in two arrays
arr_sol = {R ,G ,T , S}
arr_guess = { P, S , T ,O}

now take two counter
location_count =0
colour_count = 0

logic for the same is :
(This logic is applicable for n colours of solution)

for (i =1 to length.arr_sol) {
for (j=1 to lenght.arr_guess){
if(arr_guess[j] == arr_guess[i]) {
if(j==i) {
location_count++;
}else{
colour_count++;
}
}//end of innner for
}//end of outer for

if (location_count == lenght.arr_guess && colour_count == 0){
print”This is the right solution ”
}else {
print ” The solution is incorrect”
}
print “The number of colur and location as :
colour_count and location_count “


answer4:
I have considered all the situations and assumed that the size of the solution will be of 4. below is the code in java:
public class CowAndBull {
public static void main(String[] args) {
String solution= “1223?;
if(args.length!=1){
System.out.println(”Please input correct number of arguments, i.e 1?);
return;
}
if(args[0].length() != 4){
System.out.println(”Please input a number with only 4 digits”);
return;
}
try{
Integer.parseInt(args[0]);
}catch(NumberFormatException nfe){
System.out.println(”The given number is not integeral, please pass only integers”);
return;
}
String givenNum = args[0];
int bulls = 0;
int cows = 0;
int indexMatch = -1;
boolean[] considered = new boolean[4];
for(int i=0;i= 0 && considered[indexMatch] == false && (givenNum.charAt(indexMatch)!= solution.charAt(indexMatch))){
cows++;
considered[indexMatch] = true;
break;
}
indexMatch = solution.indexOf(givenNum.charAt(i), indexMatch+1);
}while(indexMatch >= 0);
}
}
System.out.println(”Bulls:”+bulls+”, Cows:”+cows);
}
}

(Continued on next question...)

Other Interview Questions