Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

Guess this fits into publish subscribe model - Observer design pattern

1. Each user owns his/her data in his/her user area (data structure). Can be

class User {
    UserID id;  // unique id
    std::list<Update> self_updates; // owned by user; list of updates in reverse chrono order
    std::list<Update *> friend_updates;  // owned by user friends; list of updates in reverse chronological order
    std::list<User *> friends; // Friend list
};

class Update {
    UpdateType type; // media type of this update - can be photo/video etc
    DateTime timestamp;
    Message msg; // Message text in this update
    Media *media;   // can be pointer to photo/video/audio in the update OR NULL
    List<Comment> comments;  // Comments on this update
}

Now Publish(user, msg) will
1. Wrap the msg into Update data structure and add to the front of 'self_updates' list
2. For all the friends in 'friends' list, update their 'friend_updates' list with a pointer to this Update data structure (This is where the observer pattern comes).
Note: A user logged into the FB web UI will see the update when the browser polls the user area for any self/friend updates.

GetNewsFeed(user) will
1. Get the first 30 updates from 'self_updates' and 'friend_updates' sort them according to timestamp (these two are sorted according to timestamp individually). This will interleave self_updates between friend_updates.
2. Take the latest 30 updates from this sorted list to send back to browser UI.

Hope this design suffices.

- jtr.hkcr March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

One small thing. Add userid to update class.

- andy March 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Once GetNewsFeed(user) is called, it will actually see for user friends publish() [which is actually stored just in user's account] and display 30 of them(If exist).

- uk February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If one machine capable of managing 100 accounts...then we need to have number of users/100 machines.

- uk February 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We need to implement an observer pattern, with an additonal interest of scalablity.

- Learner February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Propose an estimate based on msg operation

Publish: add msg to all the friend's msg list
GetNewsFeed: process all the msg attached to the user

Each user, there's a msg list

UserInfo {
  MessageNode *msg_list;
  FriendNode *friend_list;
}

void Publish(UserInfo user, string msg) {
  // for each friend, insert msg into their msg list
  foreach friend in friend_list
    insert msg into User[friend].msg_list;
}

void GetNewsFeed(user) {
  foreach msg in msg_list
    process msg
}

Assume
user num: 1M
avg friend_num: 1k
msg/user/second: 0.001

Consider Publish
msg insertions per second: 1/user/second, 1M total insertions/second

Consider GetNewsFeed
user num: 1M
avg friend_num: 1k
msg read operation/second/user: 1
total msg read operations: 1M

2M total msg operations/second
Assume server capacity = 1k msg operations/ server
num of servers: 2k

- Cheney March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution is slightly different from the Observer pattern.

In Observer Pattern, the publisher is the one who fires up the trigger. Suppose Obama has 100k listeners on facebook. Whenever Obama updates his status, he's publishing broadcasting updates to 100k listeners, which is not necessary(not all the listeners are active or online-waiting).

On the other hand, we modify the pattern so that observer pulls the trigger.

- Anonymous July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are they looking for this?

Search google for TAO: Facebook Distributed Data Store

- Anonymous January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are they looking for this?

Search google for TAO: Facebook Distributed Data Store

- Anonymous January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are they looking for this?

Search google for TAO: Facebook Distributed Data Store

- Anonymous January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are they looking for this?

Search google for TAO: Facebook Distributed Data Store

- Anonymous January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Are they looking for this?

Search google for TAO: Facebook Distributed Data Store

- Anonymous January 16, 2014 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More