Facebook Interview Question
348 Interview Reviews |
Back to all Facebook Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Facebook:
Given a matrix of numbers in which some may be zero. If a cell contains a zero, set all the cells in the corresponding column and row to zero.
Helpful Question?
Yes |
No
Inappropriate?



3 of 3 people found this helpful
by Sem:
function handleMatrix($input, $height, $width) {
$rowMarker = array();
$columnMarker = array();
for ($i = 0; $i < $width; $i++) {
$columnMarker[] = 0;
}
for($j=0; $j < $height; $j++) {
$rowMarker[$j] = 0;
}
for ($i = 0; $i < $width; $i++) {
for($j=0; $j < $height; $j++) {
if ($input[$i][$j] == 0) {
if ($rowMarker[$j] == 0) {
$rowMarker[$j] = 1;
markRows($input, $j, $width);
}
if ($columnMarker[$i] == 0) {
$columnMarker[$i] = 1;
markColumns($input, $i, $height);
}
}
}
}
}
function markRows(&$input, $index, $width) {
for ($i = 0; $i < $width; $i++) {
$input[$i]$index] = 0;
}
}
function markColumns(&$input, $index, $height) {
for ($i = 0; $i < $height; $i++) {
$input[$index][$j] = 0;
}
}