Honeywell Interview Question
SDE-2sCountry: United States
Interview Type: Written Test
0,0,0,0,0,0,0
0,0,1,1,1,0,0
0,0,1,0,1,0,0
0,0,1,0,1,0,0
0,1,0,0,0,1,0
0,0,1,0,1,0,0
0,0,0,1,0,0,0
Assuming the area is always bounded, it does not matter what shape it is.
* You can traverse the matrix row-by-row, look for first 1(forward traversal on row), and last 1(backward traversal on same row).
* Then you start filling all the elements in between these 2 indexes.
* If you want to make it work for complex shapes, then you can maintain a state whether you are inside the shape or outside.
For example:
0,0,0,0,0,0,1,1,1,1,0,0,0,0
0,0,1,0,0,0,1,0,0,1,0,0,0,0
0,1,0,1,1,1,1,0,0,1,0,0,0,0
0,1,0,0,0,0,0,0,0,1,0,0,0,0
0,0,1,0,0,0,1,0,0,1,0,0,0,0
0,0,0,1,0,1,0,1,0,1,0,0,0,0
0,0,0,0,1,0,0,0,1,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0
Output
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 0 1 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
Code(does not work for complex shapes)
#include <stdio.h>
typedef unsigned int uint32_t;
void print(int *arr, uint32_t rows, uint32_t cols) {
for (uint32_t i = 0; i < rows; ++ i) {
for (uint32_t j = 0; j < cols; ++ j) {
printf("%d ", arr[i * cols + j]);
}
printf("\n");
}
}
void fillObjects(int *arr, uint32_t rows, uint32_t cols)
{
for (uint32_t i = 0; i < rows; ++ i) {
uint32_t j = 0;
// left boundary
while (j < cols && arr[i* cols + j] != 1) ++ j;
uint32_t left = j;
j = cols - 1;
// right boundary
while (j > left && arr[i*cols + j] != 1) -- j;
uint32_t right = j;
// fill between maintaining state whether or
// not to fill
++ left;
while (left < right) {
arr[i * cols + left] = 1;
++ left;
}
}
}
int main() {
int arr[7*7] = {
0,0,0,0,0,0,0,
0,0,1,1,1,0,0,
0,0,1,0,1,0,0,
0,0,1,0,1,0,0,
0,1,0,0,0,1,0,
0,0,1,0,1,0,0,
0,0,0,1,0,0,0};
print(arr, 7, 7);
printf("\n");
fillObjects(arr, 7, 7);
print(arr, 7, 7);
/*
int arr2[14*9] = {
0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,1,0,0,0,1,0,0,1,0,0,0,0,
0,1,0,1,1,1,1,0,0,1,0,0,0,0,
0,1,0,0,0,0,0,0,0,1,0,0,0,0,
0,0,1,0,0,0,1,0,0,1,0,0,0,0,
0,0,0,1,0,1,0,1,0,1,0,0,0,0,
0,0,0,0,1,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
print(arr2, 9, 14);
printf("\n");
fillObjects(arr2, 9,14);
print(arr2,9,14);
*/
return 0;
}
Is there a typo? What's an arrow?? Do you mean area??? Please clarify
- prudent_programmer February 23, 2018