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;	
}

- atul June 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the logic behind above code?

- barun July 10, 2016 | Flag Reply
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

- Chirag December 19, 2020 | 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