Find Celebrity [reviewed]

Question:

You are given the 2x2 array showing who is following and who is followed.
Example) If array[0][1] = true, this means Person 0 follows person 1.
Celebrity is one that everyone follows him/her but he/she does not follow anyone.

Your algorithm should print out who is celebrity, given the 2X2 Array with followee-follower relationship.


I think I saw this question from one of Facebook interview questions. Unfortunately, I did not write down the questions. I am recollecting the questions based on my old answer.

Feel free you correct me if I am wrong.

Brute-force search algorithm examining 2x2 array will run in O(N^2). However, we wants faster than that.

*New* Matrix based approach, O(N)

UPDATE(2019-07-31): solved the problem again and realized that I was overthinking when solving this problem.

The question defines a celebrity as "Celebrity is one that everyone follows him/her but he/she does not follow anyone". If we describe celebrity in N x N matrix, $R$, it will look like below:

If 3rd person is a celebrity,

  • Condition 1: entire 3rd row will be False since he/she is not following anyone. 
  • Condition 2: 3rd column will be True except for 3rd position because everyone is following 3rd person.

Now, we need to find is to iterate over each person and check if that person's sum value of row is 0 and sum of column value is N-1.

This algorithm can be changed to find the most popular person. In that case $Condition 1$ holds and find the person who's column value is the most. In the python solution below, I assumed there can be multiple celebrities. (but there can by only one meeting the current definition).

Here is a solution in python.


Practice statistics:

07:22: to come up with algorithm
13:48: to write the code (had two bugs found after running)

DFS based approach, O(N)

This is the one type of graph search problems locating the nodes that does not any outgoing edges.
Using the Depth-First search, we might be about to search this relationship in O(N).

In this case, if we use the DFS with tracking of visited node and extra arrays to keep track of the followers.  Here is the rough algorithm

1. For each person in the array. check if each person was visited.
  1-1. If not, perform DFS on from that person.
  1-2. If the person follows other person (has outgoing edge), mark that person as follower and increase the other person's follower count by one.
  1-3. If that person was not visited yet. Perform the DFS from that person.
  1-4. move on to the next person, current person is following and perform 1-1.
2. Move on to the next person on the array.
3. Find the person(s), who is not following anyone and has positive followers.


Original C++ solution based on Depth First Search.
Remember, that there can be multiple followers.

Here is the C++ code.


Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots

Stock price processing problem