Samsung Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

"Find the shortest of the longest distance of research center from given locations of rare elements". what does it mean by shortest of the longest? can someone please explain it with an example.

- mosharafkuet April 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it means you have to find the point in the matrix which has path to all the rare element and the path which is maximum in them should be minimum from all the maximum distance from other points in the matrix from which path to each rare element is possible.

- Anonymous August 05, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<iostream>
using namespace std;
int row,col;
int a[100][100],visited[100][100],loc[100][2];
int sol=999999;
bool issafe(int x,int y) {
	return (x>=0 && x<row && y>=0 && y<col && a[x][y]==1 && visited[x][y]==0);
}
bool research(int x,int y,int x1,int y1,int count) {
	if(x==x1 && y==y1) {
		visited[x][y]=1;
		if(sol>count) {
			sol=count;
		}
		return true;
	}
	if(issafe(x,y)) {
		visited[x][y]=1;
		if(research(x,y-1,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x,y+1,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x-1,y,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x+1,y,x1,y1,count+1)==true) {
			return true;
		}
	}
	return false;
}
int main() {
	int t,index=1;
	cin>>t;
	while(t--) {
		cin>>row>>col;
		for(int i=0;i<row;i++) {
			for(int j=0;j<col;j++) {
				cin>>a[i][j];
				visited[i][j]=0;
			}
		}
		int rare;
		cin>>rare;
		for(int i=0;i<rare;i++) {
			cin>>loc[i][0]>>loc[i][1];
		}
		int longest=0,flag=0,long_index=0,maxdist[row*col];
		for(int i=0;i<row;i++) {
			for(int j=0;j<col;j++) {
				if(a[i][j]==1) {
					for(int k=0;k<rare;k++) {
						if(research(loc[k][0],loc[k][1],i,j,0)==true) {
							if(longest<sol) {
								longest=sol;
							}
							sol=INT_MAX;
						}
						else {
							flag=1;
							break;
						}
					//	memset(visited,0,sizeof(visited));
						for(int i=0;i<row;i++) {
							for(int j=0;j<col;j++) {
								visited[i][j]=0;
							}
						}
					}
					if(flag==0) {
						maxdist[long_index]=longest;
						long_index++;
					}
					else {
						flag=0;
					}
					longest=0;
				}
			}
		}
		int j=0;
		for(int i=1;i<long_index;i++) {
			if(maxdist[i]<maxdist[j]) {
				j=i;
			}
		}
		cout<<"#"<<index<<" "<<maxdist[j]<<endl;
		index++;
	}
	return 0;
}

- Prafull Singh Patel August 15, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int row,col;
int a[100][100],visited[100][100],loc[100][2];
int sol=999999;
bool issafe(int x,int y) {
return (x>=0 && x<row && y>=0 && y<col && a[x][y]==1 && visited[x][y]==0);
}
bool research(int x,int y,int x1,int y1,int count) {
if(x==x1 && y==y1) {
visited[x][y]=1;
if(sol>count) {
sol=count;
}
return true;
}
if(issafe(x,y)) {
visited[x][y]=1;
if(research(x,y-1,x1,y1,count+1)==true) {
return true;
}
if(research(x,y+1,x1,y1,count+1)==true) {
return true;
}
if(research(x-1,y,x1,y1,count+1)==true) {
return true;
}
if(research(x+1,y,x1,y1,count+1)==true) {
return true;
}
}
return false;
}
int main() {
int t,index=1;
cin>>t;
while(t--) {
cin>>row>>col;
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
cin>>a[i][j];
visited[i][j]=0;
}
}
int rare;
cin>>rare;
for(int i=0;i<rare;i++) {
cin>>loc[i][0]>>loc[i][1];
}
int longest=0,flag=0,long_index=0,maxdist[row*col];
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(a[i][j]==1) {
for(int k=0;k<rare;k++) {
if(research(loc[k][0],loc[k][1],i,j,0)==true) {
if(longest<sol) {
longest=sol;
}
sol=INT_MAX;
}
else {
flag=1;
break;
}
// memset(visited,0,sizeof(visited));
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
visited[i][j]=0;
}
}
}
if(flag==0) {
maxdist[long_index]=longest;
long_index++;
}
else {
flag=0;
}
longest=0;
}
}
}
int j=0;
for(int i=1;i<long_index;i++) {
if(maxdist[i]<maxdist[j]) {
j=i;
}
}
cout<<"#"<<index<<" "<<maxdist[j]<<endl;
index++;
}
return 0;
}

- Anonymous August 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi can you please provide flow chart of this problem statement

- RJ_007 July 20, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi can you please provide flow chart of this problem statement

- RJ_007 July 20, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>
using namespace std;
int row,col;
int a[100][100],visited[100][100],loc[100][2];
int sol=999999;
bool issafe(int x,int y) {
	return (x>=0 && x<row && y>=0 && y<col && a[x][y]==1 && visited[x][y]==0);
}
bool research(int x,int y,int x1,int y1,int count) {
	if(x==x1 && y==y1) {
		visited[x][y]=1;
		if(sol>count) {
			sol=count;
		}
		return true;
	}
	if(issafe(x,y)) {
		visited[x][y]=1;
		if(research(x,y-1,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x,y+1,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x-1,y,x1,y1,count+1)==true) {
			return true;
		}
		if(research(x+1,y,x1,y1,count+1)==true) {
			return true;
		}
	}
	return false;
}
int main() {
	int t,index=1;
	cin>>t;
	while(t--) {
		cin>>row>>col;
		for(int i=0;i<row;i++) {
			for(int j=0;j<col;j++) {
				cin>>a[i][j];
				visited[i][j]=0;
			}
		}
		int rare;
		cin>>rare;
		for(int i=0;i<rare;i++) {
			cin>>loc[i][0]>>loc[i][1];
		}
		int longest=0,flag=0,long_index=0,maxdist[row*col];
		for(int i=0;i<row;i++) {
			for(int j=0;j<col;j++) {
				if(a[i][j]==1) {
					for(int k=0;k<rare;k++) {
						if(research(loc[k][0],loc[k][1],i,j,0)==true) {
							if(longest<sol) {
								longest=sol;
							}
							sol=INT_MAX;
						}
						else {
							flag=1;
							break;
						}
					//	memset(visited,0,sizeof(visited));
						for(int i=0;i<row;i++) {
							for(int j=0;j<col;j++) {
								visited[i][j]=0;
							}
						}
					}
					if(flag==0) {
						maxdist[long_index]=longest;
						long_index++;
					}
					else {
						flag=0;
					}
					longest=0;
				}
			}
		}
		int j=0;
		for(int i=1;i<long_index;i++) {
			if(maxdist[i]<maxdist[j]) {
				j=i;
			}
		}
		cout<<"#"<<index<<" "<<maxdist[j]<<endl;
		index++;
	}
	return 0;
}

- Prafull Singh Patel August 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

U are using dfs approaches but find minimum distance means find minimum path DFS not fit for here. and your ans is wrong.
1 1 1
1 1 1
1 1 1
0 0
0 2
2 0
2 2
output must be 2;
please check the your output.

- labbi5689 February 17, 2023 | Flag


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