Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

public class HousePainting {

	int dp[][];

	public int findMinDp(int i, int j) {
		int min = Integer.MAX_VALUE;
		for (int k = 0; k < dp[i].length; k++) {
			if (k != j) {

				if (min > dp[i][k]) {
					min = dp[i][k];
				}

			}

		}

		return min;
	}

	public void bestPainting(int N, int colors[][]) {

		dp = new int[N][colors[0].length];

		for (int j = 0; j < colors[0].length; j++) {
			dp[0][j] = colors[0][j];

		}

		for (int i = 1; i < N; i++) {

			for (int j = 0; j < colors[0].length; j++) {

				dp[i][j] = findMinDp(i-1, j) + colors[i][j];

			}

		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < colors[0].length; j++)
			{
				System.out.print(" "+ dp[i][j]);
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {

		int[][] costMatrix = { { 7, 5, 10 }, { 3, 6, 1 }, { 8, 7, 4 },
				{ 6, 2, 9 }, { 1, 4, 7 }, { 2, 3, 6 }, };

		HousePainting hp = new HousePainting();

		hp.bestPainting(costMatrix.length, costMatrix);

	}
}

- akii May 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code works for the condition that the neighbors cannot have the same color. It that right?

- Anonymous July 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HousePainting {

    public static void main(String[] args) {
        getColouring(0, -1).colours.forEach(System.out::println);
    }

    private static int[][] costMatrix = {
            {7, 5, 10},
            {3, 6, 1},
            {8, 7, 4},
            {6, 2, 9},
            {1, 4, 7},
            {2, 3, 6}};


    private static Colouring getColouring(int position, int previousColor) {
        if (position == costMatrix.length) {
            return new Colouring();
        }

        Colouring cheapestColouring = null;
        int minCost = Integer.MAX_VALUE;

        for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
            if (colorIndex == previousColor) {
                continue;
            }
            Colouring nextColouring = getColouring(position + 1, colorIndex);
            int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
            if (cost < minCost) {
                nextColouring.colours.addFirst(colorIndex);
                nextColouring.totalCost = cost;
                cheapestColouring = nextColouring;
            }
        }
        return cheapestColouring;

    }

    private static class Colouring {
        LinkedList<Integer> colours = new LinkedList<>();
        int totalCost = 0;
    }
}

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

public class HousePainting {

    public static void main(String[] args) {
        getColouring(0, -1).colours.forEach(System.out::println);
    }

    private static int[][] costMatrix = {
            {7, 5, 10},
            {3, 6, 1},
            {8, 7, 4},
            {6, 2, 9},
            {1, 4, 7},
            {2, 3, 6}};


    private static Colouring getColouring(int position, int previousColor) {
        if (position == costMatrix.length) {
            return new Colouring();
        }

        Colouring cheapestColouring = null;
        int minCost = Integer.MAX_VALUE;

        for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
            if (colorIndex == previousColor) {
                continue;
            }
            Colouring nextColouring = getColouring(position + 1, colorIndex);
            int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
            if (cost < minCost) {
                nextColouring.colours.addFirst(colorIndex);
                nextColouring.totalCost = cost;
                cheapestColouring = nextColouring;
            }
        }
        return cheapestColouring;

    }

    private static class Colouring {
        LinkedList<Integer> colours = new LinkedList<>();
        int totalCost = 0;
    }
}

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

public class HousePainting {

    public static void main(String[] args) {
        getColouring(0, -1).colours.forEach(System.out::println);
    }

    private static int[][] costMatrix = {
            {7, 5, 10},
            {3, 6, 1},
            {8, 7, 4},
            {6, 2, 9},
            {1, 4, 7},
            {2, 3, 6}};


    private static Colouring getColouring(int position, int previousColor) {
        if (position == costMatrix.length) {
            return new Colouring();
        }

        Colouring cheapestColouring = null;
        int minCost = Integer.MAX_VALUE;

        for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
            if (colorIndex == previousColor) {
                continue;
            }
            Colouring nextColouring = getColouring(position + 1, colorIndex);
            int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
            if (cost < minCost) {
                nextColouring.colours.addFirst(colorIndex);
                nextColouring.totalCost = cost;
                cheapestColouring = nextColouring;
            }
        }
        return cheapestColouring;

    }

    private static class Colouring {
        LinkedList<Integer> colours = new LinkedList<>();
        int totalCost = 0;
    }
}

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

public class HousePainting {

    public static void main(String[] args) {
        getColouring(0, -1).colours.forEach(System.out::println);
    }

    private static int[][] costMatrix = {
            {7, 5, 10},
            {3, 6, 1},
            {8, 7, 4},
            {6, 2, 9},
            {1, 4, 7},
            {2, 3, 6}};


    private static Colouring getColouring(int position, int previousColor) {
        if (position == costMatrix.length) {
            return new Colouring();
        }

        Colouring cheapestColouring = null;
        int minCost = Integer.MAX_VALUE;

        for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
            if (colorIndex == previousColor) {
                continue;
            }
            Colouring nextColouring = getColouring(position + 1, colorIndex);
            int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
            if (cost < minCost) {
                nextColouring.colours.addFirst(colorIndex);
                nextColouring.totalCost = cost;
                cheapestColouring = nextColouring;
            }
        }
        return cheapestColouring;

    }

    private static class Colouring {
        LinkedList<Integer> colours = new LinkedList<>();
        int totalCost = 0;
    }

}

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

```public class HousePainting {

public static void main(String[] args) {
getColouring(0, -1).colours.forEach(System.out::println);
}

private static int[][] costMatrix = {
{7, 5, 10},
{3, 6, 1},
{8, 7, 4},
{6, 2, 9},
{1, 4, 7},
{2, 3, 6}};


private static Colouring getColouring(int position, int previousColor) {
if (position == costMatrix.length) {
return new Colouring();
}

Colouring cheapestColouring = null;
int minCost = Integer.MAX_VALUE;

for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
if (colorIndex == previousColor) {
continue;
}
Colouring nextColouring = getColouring(position + 1, colorIndex);
int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
if (cost < minCost) {
nextColouring.colours.addFirst(colorIndex);
nextColouring.totalCost = cost;
cheapestColouring = nextColouring;
}
}
return cheapestColouring;

}

private static class Colouring {
LinkedList<Integer> colours = new LinkedList<>();
int totalCost = 0;
}
}```

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

public class HousePainting {

    public static void main(String[] args) {
        getColouring(0, -1).colours.forEach(System.out::println);
    }

    private static int[][] costMatrix = {
            {7, 5, 10},
            {3, 6, 1},
            {8, 7, 4},
            {6, 2, 9},
            {1, 4, 7},
            {2, 3, 6}};


    private static Colouring getColouring(int position, int previousColor) {
        if (position == costMatrix.length) {
            return new Colouring();
        }

        Colouring cheapestColouring = null;
        int minCost = Integer.MAX_VALUE;

        for (int colorIndex = 0; colorIndex < costMatrix[0].length; colorIndex++) {
            if (colorIndex == previousColor) {
                continue;
            }
            Colouring nextColouring = getColouring(position + 1, colorIndex);
            int cost = costMatrix[position][colorIndex] + nextColouring.totalCost;
            if (cost < minCost) {
                nextColouring.colours.addFirst(colorIndex);
                nextColouring.totalCost = cost;
                cheapestColouring = nextColouring;
            }
        }
        return cheapestColouring;

    }

    private static class Colouring {
        LinkedList<Integer> colours = new LinkedList<>();
        int totalCost = 0;
    }
}

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

I dont get these abstractions. What is a house/colour/cost in these matrixes that everyone is using?

- Ad October 14, 2015 | 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