Interview Question


Country: United States




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

This is typically known as Percolation problem and widely used real life computing problems such electronics circuits,
Flow of fluid etc.
Now how do we correlate with what question is where ever there are 1's means its open site and keep moving till we find target through the 1's

Ways to solve this -
1) QuickUnion Algorithm

public class QuickUnionUF
{
private int[] id;
public QuickUnionUF(int N)
{
id = new int[N];
for (int i = 0; i < N; i++) id[i] = i;
}
private int root(int i)
{
while (i != id[i]) i = id[i];
return i;
}
public boolean connected(int p, int q)
{
return root(p) == root(q);
}
public void union(int p, int q)
{
int i = root(p);
int j = root(q);
id[i] = j;
}

}


This will find the path in linear time.

More details can be found at - Search Quick Union Find

- nirdosh.chouhan December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I assume that you can traverse the matrix in up, down, left and right direction.
Say that starting point A has coordinates A(r, c)
Check if any of neighboring points is equal 1
- up A(r-1, c)
- down A(r+1, c)
- left A(r, c -1)
- right A(r, c +1)
If any of this points is equal 1 invoke the method recursively with new coordinates eg new starting point is (r-1, c).
Repeat the process till you reach destination point or matrix's borders.
Simple code:

package interview;

public class FindPath {

    public static void main(String[] args) {

	int[][] m = { { 1, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0 }, { 0, 1, 1, 1, 0 }, { 0, 0, 0, 1, 1 } };
	System.out.println("Found path: " + findPath(0, 0, m.length - 1, m[0].length - 1, m));
    }

    private static boolean findPath(int startRow, int startCol, int endRow, int endCol, int[][] m) {

	if (startRow < 0 || startCol < 0 || endRow < 0 || endCol < 0 || startRow > m.length - 1
		|| startCol > m[0].length - 1 || endRow > m.length - 1 || endCol > m[0].length - 1) {

	    return false;
	}
	if (startRow == endRow && startCol == endCol) {
	    return true;
	}

	m[startRow][startCol] = -1;
	
	// go up
	boolean up = false;
	if (startRow - 1 >= 0 && m[startRow - 1][startCol] == 1) {
	    up = findPath(startRow - 1, startCol, endRow, endCol, m);
	}

	// go down
	boolean down = false;
	if (startRow + 1 <= m.length - 1 && m[startRow + 1][startCol] == 1) {
	    down = findPath(startRow + 1, startCol, endRow, endCol, m);
	}

	// go left
	boolean left = false;
	if (startCol - 1 >= 0 && m[startRow][startCol - 1] == 1) {
	    left = findPath(startRow, startCol - 1, endRow, endCol, m);
	}

	// go right
	boolean right = false;
	if (startCol + 1 <= m[0].length - 1 && m[startRow][startCol + 1] == 1) {
	    right = findPath(startRow, startCol + 1, endRow, endCol, m);
	}

	return up || down || right || left;
    }
}

You can also read about flood fill algorithm.

- thelineofcode December 15, 2013 | 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