## Flipkart Interview Question for SDE1s

Country: India
Interview Type: Written Test

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

Here is a sample solution for above input, can be easily converted for dynamic input:

``````#include <iostream>

using namespace std;

struct block_type
{
char data;
bool visited;
};

int main()
{
char input_array [5][5] = {{'.','.','B','.','.'},
{'.','.','G','R','R'},
{'.','.','B','.','.'},
{'R','.','.','.','.'},
{'R','.','.','.','.'}};
block_type array[5][5];
int i,j,k;
int  block_count=0;
for (i =0; i<5; i++)
{
for (j= 0; j<5; j++)
{
array[i][j].data = input_array[i][j];
array[i][j].visited = false;
}
}

for (i =0; i<5; i++)
{
for (j= 0; j<5; j++)
{
if(!array[i][j].visited)
{
if(array[i][j].data == 'B')
{
block_count++;
// mark all B and G nodes in length as visited
for (k=i; k<5; k++)
{
if(array[k][j].data == 'B' || array[k][j].data == 'G')
{
array[k][j].visited = true;
}
else
{
break;
}
}
}

if(array[i][j].data == 'R')
{
block_count++;
// mark all R and G nodes in width as visited
for (k=j; k<5; k++)
{
if(array[i][k].data == 'R' || array[i][k].data =='G')
{
array[i][k].visited = true;
}
else
break;
}
}
}
}
}

cout << "\n " << block_count << "\n";

return 0;
}``````

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

What is the logic behind above code?

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

This solution doesn't work for this case:

``````char input_array [5][5] = {{'.','.','B','.','.'},
{'.','.','G','R','R'},
{'.','.','B','.','.'},
{'B','.','.','.','.'},
{'B','.','.','.','G'}};``````

Because this 2 will miss:
1 horizontal strip from (4,4) to (4,4)
1 vertical strip from (4,4) to (4,4)
so total — 5

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.

### 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.