A was asked an interesting question on an interview lately.
- You have 1 million users
- Each user has 1 thousand friends
- Your system should efficiently answer on
Do I know him?
question for each couple of users. A user "knows" another one, if they are connected through 6 levels of friends.
E.g. A
is friend of B
, B
is a friend of C
, C
is friend of D
, D is a friend of E
, E
is a friend of F
. So we can say that, A
knows F
.
Obviously you can't to solve this problem efficiently using BFS or other standard traversing technic. The question is - how to store this data structure in DB and how to quickly perform this search.