## Samsung Interview Question for Software Engineers

Country: India

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

Ueieheiiwhsi

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

#include<iostream>
using namespace std;
int n;

bool Bipartite(int i,int color[])
{
bool yes=true;
for(int j=1;j<=n;j++)
{
if(g[i][j])// if the edge exist from i to j node
{
if(color[j]==-1)
{
color[j]=1-color[i];
yes=yes & Bipartite(j,color);
}
else if(color[j]==color[i])//if the color of adjacent node is same then its not Bipartite
return false;
}
}
return yes;
}

int main()
{
int e; //e is no of edges of the graph
cin>>n>>e; // n is the number of nodes
int color[n];
for(int i=1;i<=n;i++)
color[i]=-1; // -1 means that the node is not colored
for(int i=1;i<=e;i++)
{
int a,b;
cin>>a>>b;
g[a][b]=1; // specify the edges
g[b][a]=1;
}

for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0; // initialize the color of source node as 0
if(!Bipartite(i,color))
{
cout<<-1<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
if(color[i]=0)
cout<<i<<" ";//prints the edges with same color lets say 0 denotes red color and 1 denotes blue color
}
return 0;
}

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

#include<iostream>
using namespace std;
int n;

bool Bipartite(int i,int color[])
{
bool yes=true;
for(int j=1;j<=n;j++)
{
if(g[i][j])// if the edge exist from i to j node
{
if(color[j]==-1)
{
color[j]=1-color[i];
yes=yes & Bipartite(j,color);
}
else if(color[j]==color[i])//if the color of adjacent node is same then its not Bipartite
return false;
}
}
return yes;
}

int main()
{
int e; //e is no of edges of the graph
cin>>n>>e; // n is the number of nodes
int color[n];
for(int i=1;i<=n;i++)
color[i]=-1; // -1 means that the node is not colored
for(int i=1;i<=e;i++)
{
int a,b;
cin>>a>>b;
g[a][b]=1; // specify the edges
g[b][a]=1;
}

for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0; // initialize the color of source node as 0
if(!Bipartite(i,color))
{
cout<<-1<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
if(color[i]=0)
cout<<i<<" ";//prints the edges with same color lets say 0 denotes red color and 1 denotes blue color
}
return 0;
}

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

``````#include<iostream>
using namespace std;
int n;

bool Bipartite(int i,int color[])
{
bool yes=true;
for(int j=1;j<=n;j++)
{
if(g[i][j])// if the edge exist from i to j node
{
if(color[j]==-1)
{
color[j]=1-color[i];
yes=yes & Bipartite(j,color);
}
else if(color[j]==color[i])//if the color of adjacent node is same then its not Bipartite
return false;
}
}
return yes;
}

int main()
{
int e; //e is no of edges of the graph
cin>>n>>e; // n is the number of nodes
int color[n];
for(int i=1;i<=n;i++)
color[i]=-1; // -1 means that the node is not colored
for(int i=1;i<=e;i++)
{
int a,b;
cin>>a>>b;
g[a][b]=1; // specify the edges
g[b][a]=1;
}

for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0; // initialize the color of source node as 0
if(!Bipartite(i,color))
{
cout<<-1<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
if(color[i]=0)
cout<<i<<" ";//prints the edges with same color lets say 0 denotes red color and 1 denotes blue color
}
return 0;
}``````

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

#include<iostream>
using namespace std;
int n;

bool Bipartite(int i,int color[])
{
bool yes=true;
for(int j=1;j<=n;j++)
{
if(g[i][j])// if the edge exist from i to j node
{
if(color[j]==-1)
{
color[j]=1-color[i];
yes=yes & Bipartite(j,color);
}
else if(color[j]==color[i])//if the color of adjacent node is same then its not Bipartite
return false;
}
}
return yes;
}

int main()
{
int e; //e is no of edges of the graph
cin>>n>>e; // n is the number of nodes
int color[n];
for(int i=1;i<=n;i++)
color[i]=-1; // -1 means that the node is not colored
for(int i=1;i<=e;i++)
{
int a,b;
cin>>a>>b;
g[a][b]=1; // specify the edges
g[b][a]=1;
}

for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0; // initialize the color of source node as 0
if(!Bipartite(i,color))
{
cout<<-1<<endl;
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
if(color[i]==0)
cout<<i<<" ";//prints the edges with same color lets say 0 denotes red color and 1 denotes blue color
}
return 0;
}

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.