Lesson 1 · Minutes 0-10

Search Autocomplete: Scope

Problem

Two hundred milliseconds is a long time for a server and a short time for a network, and the first ten minutes go on finding that out. You have a question with a deadline in it and no numbers behind it. By minute ten the interviewer should have a request rate, a budget with the network already taken out, one sum that rules out searching at query time, and an API of one call.

The question's own words do some of the work. "Before the next letter" is a requirement waiting to be turned into a number, and "most popular" is a question nobody has answered. Both need settling before you draw.

Five Answers the Design Waits On

Each of these has an answer that moves something on the board, and the interviewer should hear what it moves.

  1. How many searches a day, and how many letters in each? The product is the request rate, because every letter is a request. Agreed: 500 million searches a day, and about 6 letters typed in each before a suggestion is picked.
  2. How long is "before the next letter"? That is the budget everything has to fit. Agreed: assume a fast typist's 200 milliseconds.
  3. Does everyone see the same five? A yes makes the answers precomputable, and cacheable by anyone on the path. Agreed: yes, and personal suggestions wait for a curveball.
  4. What does popular mean, and how fresh must it be? All-time counts and today's trend are different systems. Agreed: finished searches over the last 30 days, with a new index once a day.
  5. Prefix only, or typos and the middle of a phrase too? Typos need a tree you can walk. Agreed: prefix only, in one language, with a block list of terms never to suggest, applied at build time.

Two of these deserve to be said in full, because they tell the interviewer what you plan to do with the answer. On the budget, the question carries its own estimate: "How long do I have, exactly? If it's before the next letter, that's about 200 milliseconds, and most of it is the network." On the five, make it a proposal they can refuse: "If everyone sees the same five, I can compute them ahead of time and let anything along the path hold them. Is that fair?"

Requirements and What Stays Out

The functional list is short: return up to five completions for a prefix, and count finished searches. Three non-functional requirements carry the weight. An answer arrives before the next letter. The box keeps answering while an index is built. Suggestions may lag by a day or so, and that last line pays for most of what follows.

Leave out the search results themselves, ranking beyond popularity, typo correction and personal history, and leave them out in a sentence the interviewer can object to. Any of them would draw a different system, and personal history is back later as a curveball.

The Letter as the Unit of Load

Every rate here starts from the letter, not the search. Five hundred million searches at 6 letters each is 3 billion requests a day. Divide by about 100,000 seconds, and that is about 30,000 a second. At a peak of twice the average it is 60,000 a second, and the fast path has to carry that.

Counted this way, a search is one write, the line it leaves in the log, and each of its letters is one read. So the log takes about 5,000 lines a second, and 10,000 at peak. Sixty thousand reads would fit one machine's 100,000 simple operations a second, if a lookup were one simple operation. The subtree sum, two sections on, says whether it is.

The letters, and what they ask for
Writes500 million logged searches a day
About 5,000 a second, 10,000 at peak. One log line each.
Reads6 letters per search
About 30,000 a second, 60,000 at peak. If a lookup were one simple operation, 1 server at 100,000 a second.
Bandwidth in100 bytes a log line
About 500 KB a second, 1 MB at peak. Decides nothing.
Bandwidth out500 bytes an answer
About 15 MB a second, 30 MB at peak. Decides nothing.

Two Hundred Milliseconds, Spent

Now turn the question's phrase into milliseconds, and take the network out first, because that part is not yours to change. The gap between two letters is 200 milliseconds. A round trip across a continent, US east to US west, is about 70. Assume 30 for the server. The other 100 belong to a slow connection and to drawing the list, so nothing is planned on them yet.

Some readers are over budget before a server does anything. Put your servers in US east, and a reader in Southeast Asia has a round trip of about 225 milliseconds, longer than the whole gap. Nothing on the server fixes that. Name it and park it until the interviewer gets to the client.

Where 200 milliseconds goes
The gap200 ms
→
Round tripabout 70 ms
→
Server30 ms
→
What is leftabout 100 ms

The Subtree That Decides

A trie is where everyone starts: a tree with one node per letter, so that the path down to a node spells a prefix. Size it first. Assume 50 million distinct queries of about 24 characters each. Shared prefixes leave about 10 new nodes a query, so that is about 500 million nodes. Assume 50 bytes a node, and it comes to 25 GB, which fits one machine of about 100 GB. Nothing is decided yet.

What decides is one subtree, everything beneath a single prefix. Assume a common first letter holds one node in 15, about 33 million nodes. Visiting a node is a memory access of about 100 nanoseconds, so visiting them all takes about 3 seconds. The server has 30 milliseconds. One keystroke costs a hundred times its allowance, so at this size nothing can be searched at query time, and that sum is what the rest of the round answers to.

Two Sums to Drop

Two more sums take a breath each, and the reason to say them is to drop them. Sixty thousand answers a second, each about 500 bytes, make 30 MB a second, against 1.25 GB a second on a 10-gigabit link, which is too small to matter. The search log is 500 million lines at about 100 bytes, or 50 GB a day, about twice the trie. The slow path is a modest batch job, and it needs no more sizing in the room.

One Call, Two Entities

The API is one call. A GET on the suggest path, with the prefix as its only parameter, returns up to five strings. There is no call to count anything, because the search itself, which this problem does not design, writes a line to the log when it finishes.

The call and the two entities
GET /suggest?q=ap
    200 ["apple", "apple watch", "app store", "apartments", "apple music"]

query count    query, weighted count
index entry    prefix, five completions, index version

Behind the call sit two entities. A query count holds a query and its weighted count, and the slow path produces it. An index entry holds a prefix, its five completions and the version of the index it belongs to, and the fast path reads it. The two meet only where one is turned into the other, and in twenty minutes that fact is doing a lot of work.

Interview Pitfalls
  • Counting searches and forgetting that each one is six requests. The rate comes out six times too small, and every later sum inherits it.
  • Never turning "before the next letter" into milliseconds, which leaves later choices nothing to answer to.
  • Assuming suggestions are personal without asking. That one word removes every cache on the path.
  • Spending minutes on the size of the search log, which decides nothing while the interviewer waits for the budget.
What Interviewers Listen For
  • The question's phrase turned into a budget, with the network subtracted first.
  • One subtree priced against that budget from the latency ladder, not described as slow.
  • The question whether everyone sees the same five, asked for what it buys.

Knowledge Check

You have time for one more question before you draw. Which answer makes precomputing the completions possible?

  • How many distinct queries the search log holds in a normal month
  • How many bytes one answer takes once it is out on the wire
  • Whether every user sees the same five completions for a prefix
  • Which letters users type most often at the start of a search

There are 500 million searches a day at 6 letters each. What must the fast path carry at a peak of twice the average?

  • About 10,000 a second, one request for each search in the log
  • About 30,000 a second, with every letter counted as a request
  • About 120,000 a second, with a second request for each answer
  • About 60,000 a second, since each letter is a request of its own

The gap between letters is 200 milliseconds and a round trip across a continent is about 70. Why does the server get only 30?

  • A server cannot do useful work in more than 30 milliseconds
  • The rest is held back for slow connections and for drawing
  • A lookup in memory always takes 30 milliseconds to complete
  • All but 30 of the gap is used up crossing the network itself

Which estimate do you say in one breath and then drop, because it decides nothing here?

  • The 3 seconds it takes to search under a common first letter
  • The 60,000 letters a second that arrive at peak traffic
  • The 200 milliseconds between one letter and the next
  • The 30 MB a second of answers against one 10-gigabit link

You got correct