## Facebook Interview Question

Android Engineers**Country:**United States

**Interview Type:**In-Person

We could represent the persons as the array of Qubits:

00: Celebrity - knows nobody in the room

01: No celebrity - knows only celebrity

10: No celebrity - knows everybody but no celebrity

11: No celebrity - knows the celebrity and everybody else

If we sort the array - then we know where to look for the celebrity.

Based on your question, the problem is asking us to find the celebrity given the person list and the person number. So, this can be done in O(N+N) = O(N) time where N = number of rows/cols. If this was problem was finding the celebrity given the matrix, then it would be a different problem. I present a solution in Python below.

I assume I am given an adjacency matrix. My solution in Python:

```
def determineCelebrity(peopleList, person):
# Scan the adjacency matrix
actualPersonIndex = person - 1
# Scan the cols and make sure everyone knows the person
for i in range(len(peopleList)):
if i != actualPersonIndex and peopleList[i][actualPersonIndex] != 1:
return False
# Make sure the celebrity knows no one else in the party
return all(x == 0 for x in peopleList[actualPersonIndex])
```

Test code:

```
celebrityMatrix = \
[
[0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0]
]
print(determineCelebrity(celebrityMatrix, 5)) # False
print(determineCelebrity(celebrityMatrix, 2)) # True
print(determineCelebrity(celebrityMatrix, 3)) # False
```

1. Given problem of finding whether the given person is celebrity or not can be solved in 0(n) just by checking row corresponding to that person should be all 0's, and column should be all 1's.

2. If the problem is to find the celebrity, then also complexity will be O(n), ie. starting from the first person, iterate it's row and find out the first 1 say at element i, all elements/persons before that one cannot be celebrity as first person does not know them, so direct skip to i, and start iterating its row from i+1 th position and keep skipping .

```
class FindTheCeleb
{
public static void main (String[] args) throws java.lang.Exception{
/*
Assumption: whoIknow matrix
Everyone knows themselves, so a 1 for themselves
Everyone may or maynot know the others in the party thus 0/1 for the rest of the fields
*/
Integer[][] whoIknow=new Integer[][]{{1,0,0,0,0,0},
{1,1,0,0,1,0},
{1,1,1,0,0,0},
{1,0,0,1,1,0},
{1,0,0,1,1,0},
{1,0,0,1,1,1}};
Integer[] people= new Integer[]{1,2,3,4,5,6};
Integer testPerson1 = 4;
Integer testPerson2 = 1;
System.out.println("Person "+ testPerson1 +
(isCelebrity(whoIknow, testPerson1)? " is a isCelebrity": " is not a celebrity"));
System.out.println("Person "+ testPerson2 +
(isCelebrity(whoIknow, testPerson2)? " is a isCelebrity": " is not a celebrity"));
}
public static boolean isCelebrity(Integer[][] whoIknow, Integer testPerson){
int i=0;
for(;i<whoIknow.length;i++){
//System.out.println(whoIknow[i][testPerson-1]);
if(whoIknow[i][testPerson-1]==0){
//System.out.println("testPerson=" + testPerson);
return false;
}
}
return true;
}
}
```

- mvb13 March 13, 2018