☕ The Engineering Behind Facebook Newsfeed

Hi Everyone,

Hope you’re all having a fantastic day!

Today we’ll be talking about

  • The engineering behind Facebook Newsfeed

  • The big policy change on remote work at Big Tech companies

Today’s coding question is from Microsoft (I got this exact question in a Microsoft interview I did).

The previous solution is on evaluating an expression given in Reverse Polish Notation.

Interviewing.io is an awesome resource.

Book realistic mock interviews with senior FAANG engineers who will give you detailed and actionable feedback on exactly what you need to work on.

They prepare you for the pressure and stress that comes from an actual interview setting.

You don’t pay anything until you’re hired.

Check them out here.

Tech Blog

How ML powers Facebook’s News Feed

How Facebook uses ML for Newsfeed

When you log onto Facebook, there are thousands of possible posts/comments/updates that Facebook could display for you.

In order to rank them, Facebook first goes through every post and calculates the probability that you’ll engage with it.

One observable signal for engagement is if you click the like button. So, Facebook calculates the probability that you’ll like the given post based on a multitude of factors (how many other people have liked the post, what’s the relationship between you and the poster, how many times have you liked previous content by the poster, etc.)

However, liking isn’t the only way you can engage with the content. You could also share it, leave a comment or (if it’s a video) watch the entire video.

Therefore, Facebook runs multiple prediction models that calculate the probability that you will like, share, comment, etc. for every piece of content that they’re ranking in your newsfeed.

They then combine these probabilities into an overall factor that they use to rank the content in your newsfeed.

How does Facebook Implement This?

Facebook now has to implement that machine learning algorithm for all their users. More than 2 billion people where each user has more than 1,000 posts that need to be ranked per day.

They also have to do this in real time! When you refresh your newsfeed, you’ll want to see status updates from your closest friends posted several minutes ago. Only displaying content that was posted several hours/days ago wouldn’t be a good user experience.

In order to do this, Facebook uses a feed aggregator to collect all relevant information about a post and analyze all it’s features. The feed aggregator then predicts the engagement probabilities and uses that to calculate a ranking score for your news feed.

Whenever you refresh your Facebook newsfeed, Facebook’s Web/PHP layer will then query the feed aggregator for the posts it should display. The feed aggregator queries the feed leaf databases for all the posts and relevant metrics. The feed aggregator then crunches the numbers and returns a ranked list of posts for the front end to render.

Industry News

The future of Remote Work in Big Tech

Many Big Tech companies have been amending their policies on remote work over the past few weeks.

They’re mostly allowing for more flexibility around remote work.

Here’s a breakdown of company policy…

Facebook

Facebook is one of the most remote-friendly Big Tech companies.

Previously, their policy was that certain employees, notably the most senior and experienced employees, could request permanent remote work.

Now, they’ve changed their policy to allow employees at all levels to work remotely full time.

For employees who choose to work in an office, they will still be allowed a hybrid schedule as long as they come to the office at least half the time.

Amazon

Amazon will be allowing employees to work remotely up to 2 days per week. Leadership teams at Amazon will determine what days employees will be required to work from the office.

Employees who don’t want to work in the office three days per week will be able to apply for an exception.

Also, some employees will have the option to work fully remote for up to four weeks out of the year.

This is a significant shift from Amazon’s stance in March of this year when they stated that their goal was to return to an office-centric culture.

The company received negative feedback from this stance and has now shifted to be more remote friendly.

Apple

Apple has moved towards a hybrid work policy, with CEO Tim Cook saying most employees would return to the office on set days (Monday, Tuesday and Thursday) and be allowed to WFH on Wednesdays and Fridays.

Apple is also offering a two-week work-from-anywhere benefit.

However, many Apple employees pushed back on this announcement and asked for permanent remote work and location flexibility.

Google

Google’s initial plan was for all employees to return to the office at least three days a week. However, Google walked back that plan last week and adopted a more flexible model.

Googlers can continue working remotely until September, after which they can either go back to their pre-pandemic office, work out of a Google office in a different city, or work remote permanently if their role allows it.

Google CEO Sundar Pichai said he expects 60% of the company’s global workforce to return to the office a few days a week. 20% of employees will move to a different office and the remaining 20% will be working remote permanently.

Google also doubled their “work-from-anywhere” allowance to 4 weeks, so employees can spend up to 20 days a year working from any location.

Netflix

Netflix CEO Reed Hastings has been the most prominent voice against remote work and he said that Netflix employees should plan to be back at work after Labor Day (September 6th).

Spotify

Spotify is extremely pro-remote work and is allowing employees to work from anywhere as long as their location doesn’t throw up legal barriers and the time zone makes sense.

Spotify will also be paying employees the same regardless of what their location is.

Interview Question

You are given a linked list with n nodes.

The nodes in the linked list have a next and prev pointer, but they also have a random pointer.

The random pointer points to a randomly selected node in the linked list (or it could point to null).

Construct and return a deep copy of the given linked list.

We’ll send a detailed solution tomorrow, so make sure you move our emails to primary, so you don’t miss them!

Gmail users—move us to your primary inbox

  • On your phone? Hit the 3 dots at the top right corner, click "Move to" then "Primary"

  • On desktop? Back out of this email then drag and drop this email into the "Primary" tab near the top left of your screen

Apple mail users—tap on our email address at the top of this email (next to "From:" on mobile) and click “Add to VIPs”

Previous Solution

As a refresher, here’s the last question

You are given an array that contains an expression in Reverse Polish Notation.

Return the result from evaluating the expression. You can assume the expression will always be valid.

Input - [“2”, “3”, “+”, “3”, “*”]

Output - 15 because ( (2 + 3) * 5)

Input - [“4“, “5“, “/“, “30“, “+“]

Output - 30.8 because ( (4 / 5) + 30)

Here’s the question in LeetCode.

Solution

The key to solving this question is a stack.

Remember, Stacks are Last In, First Out.

So they work perfectly for Reverse Polish Notation.

We iterate through our input array and run an if condition

  • if the current element is an integer, add it to the stack

  • if the current element is an operation (+, /, * or -) then pop the last 2 numbers off the stack, apply that operation, and then push the result onto the stack

After iterating through the array, we should be left with only 1 number in our stack.

That’s the answer!

If you want to practice more questions like this with top engineers at Google, Facebook, etc. then check out Interviewing.io.

You can book realistic mock interviews with senior FAANG engineers who will give you detailed and actionable feedback on exactly what you need to work on.

You don’t pay anything until you’re hired.

Check them out here.