Final challenge: a feed for another app
After this: Estimate, compare push and pull, run a hybrid, predict a fan-out and answer a follow-up for a new app.
This challenge has five steps, and each step is scored on its own. A new app, a photo app with 2 million people a day, needs a feed. You will estimate its feed loads, compare push and pull with its numbers, run a hybrid by hand, predict what the fan-out code prints, and answer an interviewer's follow-up. The step with code shows Python and Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Feed loads per second
Feed loads per second
A photo app has 2 million people a day. Each makes 1 post and opens the feed 30 times a day, and has 100 followers and 100 followed accounts on average. About how many feed loads arrive per second, on average?
Push or pull
A photo app has 2 million people a day. Each makes 1 post and opens the feed 30 times a day, and has 100 followers and 100 followed accounts on average. How does the work of fan-out on read compare with fan-out on write?
Run the hybrid
A hybrid with a limit of 3 followers. Ana (followers Ben and Cy) posts p1. Bo (followers Ana, Ben, Cy, Dee and Eve) posts p2. Cy (follower Ben) posts p3. Then Ben opens his feed. How many fan-out jobs, feed inserts and pulled posts are there?
Predict the feeds
The fan-out code from lesson 2, with three posts. What does it print?
followers = { "Ana": ["Ben", "Cy", "Dee"], "Cy": ["Ana", "Ben"],}feeds = {}
def fan_out(author, post_id): for person in followers[author]: if person not in feeds: feeds[person] = [] feeds[person].insert(0, post_id) # newest first return len(followers[author])
inserts = fan_out("Cy", "p1") + fan_out("Ana", "p2") + fan_out("Cy", "p3")print(inserts)print(" ".join(feeds["Ben"]))print(" ".join(feeds["Ana"]))import java.util.*;
public class Main { static Map<String, List<String>> followers = Map.of( "Ana", List.of("Ben", "Cy", "Dee"), "Cy", List.of("Ana", "Ben")); static Map<String, List<String>> feeds = new HashMap<>();
static int fanOut(String author, String postId) { for (String person : followers.get(author)) { if (!feeds.containsKey(person)) { feeds.put(person, new ArrayList<>()); } feeds.get(person).add(0, postId); // newest first } return followers.get(author).size(); }
public static void main(String[] args) { int inserts = fanOut("Cy", "p1") + fanOut("Ana", "p2") + fanOut("Cy", "p3"); System.out.println(inserts); System.out.println(String.join(" ", feeds.get("Ben"))); System.out.println(String.join(" ", feeds.get("Ana"))); }}A follow-up question
Twenty minutes in, your photo app uses fan-out on write for everyone. The interviewer asks: a famous photographer with 30 million followers joins and posts every hour. What do you say?
- Estimate feed loads for a new app, compare push and pull with numbers, run a hybrid, predict a fan-out, and answer a celebrity follow-up.