Zillow Interview Question
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZillowInterViewProblems
{
class Program
{
static void Main(string[] args)
{
int location;
int color;
Program.GetSolution(args[0],args[1],out location,out color);
Console.WriteLine("Location: " + location+" Color: " + color);
}
public static void GetSolution(string solution,string guess,out int location,out int color)
{
location = 0;
color = 0;
if (solution == guess)
{
location = solution.Length;
color = 0;
return;
}
for (int i = 0; i < solution.Length;i++ )
{
if (guess[i] == solution[i])
{
location++;
}
else if(solution.Contains(guess[i]))
{
color++;
}
}
}
}
}
public void chkLocationColor()
{
ArrayList<String> a1 = new ArrayList<String>();
a1.add("B");
a1.add("G");
a1.add("R");
a1.add("R");
ArrayList<String> a2 = new ArrayList<String>();
a2.add("R");
a2.add("G");
a2.add("Y");
a2.add("Y");
int iLocation = 0;
int iColor = 0;
if(a1.equals(a2))
{
System.out.println("4 Location : 4 Color");
}
else
{
for(int i=0;i<a2.size();i++)
{
if(a1.get(i).equals(a2.get(i)))
{
iLocation++;
}
else if(a1.contains(a2.get(i)))
{
iColor ++;
}
}
System.out.println("Location : " + iLocation + " Color: " + iColor);
}
Output: Location : 1 Color: 1
package test;
public class GuessLocationColor {
/**
* @param args
*/
public static void main(String[] args) {
Guess solution = new Guess(0, 1, 2, 2);
Guess g1 = new Guess(2, 1, 3, 3);
Guess g2 = new Guess(0, 1, 2, 2);
System.out.println(g1.getResponse(solution));
System.out.println(g2.getResponse(solution));
}
}
class Guess {
private static int BALL_SIZE = 4;
private static int COLOR_SIZE = 4;
public int[] balls = null;
public Guess() {
balls = new int[BALL_SIZE];
}
public Guess(int... args) {
balls = new int[BALL_SIZE];
if (args.length == BALL_SIZE) {
for (int i = 0; i < args.length; i++) {
balls[i] = args[i];
}
}
}
public Response getResponse(Guess solution) {
int locationMatch = 0;
int colorMatch = 0;
int[] guessColors = new int[COLOR_SIZE];
int[] solutionColors = new int[COLOR_SIZE];
for (int i = 0; i < BALL_SIZE; i++) {
if (balls[i] == solution.balls[i]) {
locationMatch++;
} else {
int guessColor = balls[i];
int solutionColor = solution.balls[i];
guessColors[guessColor]++;
solutionColors[solutionColor]++;
}
}
for (int i = 0; i < COLOR_SIZE; i++) {
int min = Math.min(guessColors[i], solutionColors[i]);
colorMatch += min;
}
Response response = new Response();
response.locationMatch = locationMatch;
response.colorMatch = colorMatch;
return response;
}
}
class Response {
int locationMatch;
int colorMatch;
public String toString() {
return "location = " + locationMatch + ", color = " + colorMatch;
}
}
string checkGuess(string s1, string s2){
if(s1.length()==0)
return "Invalid Condition";
if(s1.length()>s2.length())
return "Incomplete guess";
int num_location=0, num_color=0;
map<char,int> color_count;
for(int i=0;i<s1.length();i++){
if(s1[i]==s2[i]){
num_location++;
s2[i]='%';
}
else
color_count[s1[i]]++;
}
for(int i=0;i<s1.length();i++){
if(s2[i]!='%' && color_count[s2[i]]>0){
num_color++;
color_count[s2[i]]--;
}
}
string re="Location: ";
re+=num_location+'0';
re+=" Color: ";
re+=num_color+'0';
return re;
}
static class LocationColorPair {
int location, color;
}
static LocationColorPair countLocationColor(String search, String find) {
if(search == null || find == null || search.length() != find.length()) return null;
LocationColorPair result = new LocationColorPair();
int[] available = new int[4];
int[] machable = new int[4];
for(int pos = 0; pos < search.length(); pos++) {
if(search.charAt(pos) == find.charAt(pos)) {
result.location++;
} else {
available[mapToIndex(search.charAt(pos))]++;
machable[mapToIndex(find.charAt(pos))]++;
}
}
for(int i=0; i<4; i++) {
result.color += Math.min(available[i], machable[i]);
}
return result;
}
static int mapToIndex(char ch) {
switch(ch) {
case 'B': return 0;
case 'G': return 1;
case 'R': return 2;
case 'Y': return 3;
default:
throw new IllegalArgumentException();
}
}
I think depending on how the interviewer expects the answer to be returned, we could have a case where the Solution is "BGRR" and the guess is "RRRR". In this case, it might be better to return "Location : 2" and "Color : 0" since we've already guessed the correct two "R" positions. In that case, pre-process first and then loop for the color count:
public static String validateGuess(String solution, String guess) {
if (solution == null || guess == null) return null;
if (solution.length() < 4 || solution.length() > 4 || guess.length() < 4 || guess.length() > 4) return null;
int locationCount = 0; // count of correct color in the correct location
int colorCount = 0; // count of correct color in the incorrect location
if (guess.equalsIgnoreCase(solution)) return "Location: " + locationCount + " | Color: " + colorCount;
boolean[] correctGuesses = new boolean[solution.length()];
for(int i = 0; i < guess.length(); i++) {
char currentChar = guess.charAt(i);
if (solution.indexOf(currentChar) != -1) {
if (currentChar == solution.charAt(i)) {
locationCount++;
correctGuesses[i] = true;
}
}
}
// now that we know all the correct guesses, calculate the color count:
for(int i = 0; i < guess.length(); i++) {
char currentChar = guess.charAt(i);
int index = solution.indexOf(currentChar);
if (index != -1) {
if(!correctGuesses[index]) {
colorCount++;
}
}
}
return "Location: " + locationCount + " | Color: " + colorCount;
}
public void evalResponse(String s, String g) {
// for sloppy purpose, I assume that s and g are at least valid
Map<Character, Integer> map = new TreeMap<Character, Integer>();
int loc = 0;
for (int i = 0; i < s.length; i++) {
if (s.charAt[i] == g.charAt[i]) {
loc++;
}
if (map.containsKey(s.charAt[i])) {
map.put(s.charAt[i], s.get(s.charAt[i]) + 1);
} else {
map.put(s.charAt[i], 1);
}
if (map.containsKey(s.charAt[i])) {
map.put(s.charAt[i], s.get(s.charAt[i]) - 1);
} else {
map.put(s.charAt[i], -1);
}
}
int color = 0;
for (key : map.keySet()) {
color += Math.max(map.get(key), 0);
}
color -= loc;
// loc and color are the answers now
}
- Anonymous November 27, 2014