Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Just like a graph traversal using a same size matrix with initial value set to -1. Mark the position you visited and, if you see a cycle (not -1 value), you know you will not be able to visit all the cells.

time complexity nxn and space complexity nxn

- qxixp February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it is null-terminated, then finding a cycle means we can't reach all cells. Otherwise, the cycle must be of length "nxn". Also, we must make sure the traversal takes "nxn", i.e., the graph is connected or technically every vertex is reachable from (0, 0).

I guess we can also do this in O(n^2) time with O(1) extra space. It is similar to finding a cycle in a sequence (Floyd's cycle finding algorithm = hare-tortoise).
If there is a cycle then is has to be of length "nxn".
If there are no cycles, then the tortoise has to have walked "nxn" cells.

- Ehsan February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

You don't need extra space. You can use the same matrix. Set the entry to (n+1,m+1) once you visit it. If you ever encounter an entry with(n+1,m+1), break the traversal. Then check every entry of matrix. If all of them are (n+1, m+1), you have traversed every entry otherwise no.

- aj February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

aj's solution is correct. But the final traversal to check whether all cells have been visited is not necessary. We can maintain a variable 'count' to hold the number of visited cells. When we encounter a visited cell, just check 'count==M*N'

- walnutown April 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

No extra space needed. But instead of change it to (n+1, m+1), change it to (-a, -b). In that case, we can revert it back after traverse.

- StubbornLeaf April 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

$A = array(
	array(
		array(0,1),array(1,2),array(3,3)
	),
	array(
		array(1,1),array(3,3),array(3,2)
	),
	array(
		array(3,0),array(1,3),null
	),
);

$hasBeen = array();
$rootPosition = array(0,0);
$total = 0;
var_dump(func($A,$rootPosition,$total,$hasBeen));


function func($A,$position,$total,$hasBeen) {
	$item = $A[$position[0]][$position[1]];

	if ($item == null) {
		return $total == count($A)*count($A);
	}
	
	$hasBeen []= $position;
	
	$nextPosition = array($item[0],$item[1]);

	if (!in_array($nextPosition,$hasBeen)) {
		return func($A,$nextPosition,$total+1,$hasBeen);
	}
	
	return false;
}

- giladsoffer February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

by "3,3" you must mean "0,0". otherwise array index out of bound exception.

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

what do you mean by follow the cell location. Say M[0][1] is 1, what does it mean?

- Avan May 27, 2014 | 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