Amazon Interview Question
Software Engineer / DevelopersIf the given graph is a line ,it should satisfy the following conditions:
1) Its should be connected-->for checking connectivity use dfs or bfs
2) Except starting and end vertes, all other vertex should have degree=2 with indegree=1 && outdegree=1
for starting vertex outdegree=1 && indegree=0
for end vertex indegree=1 && outdegree=0
seems to me all we need are three rules
1. no self connection: matrix[i][i] =0
2. check all the entry in matrix[i][j] that i<j, each row matrix[i] should have at most one entry as 1
3. the total entries that is 1 in the matrix[i][j] that i<j is n-1
Would the above three rules be suffice?
seems to me all we need are three rules
1. no self connection: matrix[i][i] =0
2. check all the entry in matrix[i][j] that i<j, each row matrix[i] should have at most one entry as 1
3. the total entries that is 1 in the matrix[i][j] that i<j is n-1
Would the above three rules be suffice?
well, you can pick the first row of the matrix and locate the entry with 1, if it was the jth entry, go to the jth row and look for a 1 and continue so on till you hit a row with zero 1's.
Now take the 1st column and look for a 1, basically the same routine as above but with columns this time.
Also whenever you visit a column or a row make sure that the number of 1's in it is less than 2, if not return false
At any point of time keep a count of the jumps(row2row or col2col) you made, if the count is greater than n (basically this a check for a loop ), return false. If at the end of all the jumps the count is less than n return false (implies the straight line not complete)
In all other cases return true
P.S: the definition of number of jumps depends on how u count it. so, n might not be the appropriate bound
Can we try adding the row values and verify the idea of "idea" above...
1----4----2----3
Matrix | RowSum
0001 = 1
0011 = 2
0100 = 1
1100 = 2
||||
1212 <- Col Sum
No row can have sum 0
Only 2 rows can have sum = 1
The sequence of row count should be same 1212 ~ 1212
Diagonals = 0
And Symmetric Matrix
i think guys here have already discussed about the solution i'm writing. It's
O(n2). Is there any lower order solution ?
bool isStraightLine(bool edges[N][N])
{
int outEdges[n];
int inEdges[n];
for (int i =0; i < 0; i++) {
inEdges[i] = outEdges[i] = 0;
}
for (int i=0; i < N; i++) {
for(int j =0; j < N ; j++) {
outEdges[i] += edges[i][j];
inEdges[j] += edges[i][j];
}
}
bool straightLine = true;
for (int i =0; i < N; i++) {
if ((inEdges[i] > 1) || (outEdges[1] > 1)) {
straightLine = false;
break;
}
}
return straightLine;
}
i think guys here have already discussed about the solution i'm writing. It's
O(n2). Is there any lower order solution ?
bool isStraightLine(bool edges[N][N])
{
int outEdges[n];
int inEdges[n];
for (int i =0; i < 0; i++) {
inEdges[i] = outEdges[i] = 0;
}
for (int i=0; i < N; i++) {
for(int j =0; j < N ; j++) {
outEdges[i] += edges[i][j];
inEdges[j] += edges[i][j];
}
}
bool straightLine = true;
for (int i =0; i < N; i++) {
if ((inEdges[i] > 1) || (outEdges[1] > 1)) {
straightLine = false;
break;
}
}
return straightLine;
}
Above poster got it mostly correct, but a little needlessly convoluted.
Two things need to be true.
1.) There should be N-1 edges;
2.) No node has more than one outgoing edge;
- Anonymous July 22, 2009