Zillow Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ColorSolution {
	
	public static void getSolution(String str1, String str2) {
		if(str1 == null || str2 == null) {
			return;
		}
		else if(str1.length() != str2.length()) {
			return;
		}
		else {
			int i=0, location = 0, color = 0;
			if(str1.equalsIgnoreCase(str2)) {
				location = str1.length();
				return;
			}
			while(i<str1.length()) {
				if(str1.charAt(i) == str2.charAt(i)) {
					location++;
				}
				else {
					String temp = "" + str2.charAt(i);
					if(str1.contains(temp)) {	
						color++;
					}
				}
				i++;
			}
			System.out.println(location + " Location " + color + " Color" );
			return;
		}
	}
	
	public static void main(String args[]) {
		String str1 = "BGRR";
		String str2 = "RGYB";
		getSolution(str1, str2);
	}
}

- Anonymous November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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++;
}
}

}

}
}

- Slaven Slugic September 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//algorithm

(1)make a trie of all possible combinations
(2)pick the first one(or random) string from the trie and gets the 'match' and 'color'
(3)knowing match and color, remove all possible combinations which is not matching with the 'match' and 'color'

- mail2vcp@gmail.com October 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Siri July 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}
}

- Lei Chen October 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Anonymous January 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
    }
  }

- xelways March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
    }

- tmgunternet April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 January 13, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More