download

Why retrieve candidates before ranking, and which stage missed the item?

Machine learning~12 minReviewed 29 Oct 2026Free · No sign-in

Question

An outdoor shop shows 3 recommended products on its home page. Stage 1, retrieval, picks 6 candidates out of 2 million products with a fast embedding search. Stage 2, a slower ranking model, orders those 6 and shows the top 3.

One shopper would really buy 4 products: tent, stove, lamp and mat.

  • Retrieval returned: socks, tent, mug, lamp, cap, rope.
  • The page showed: tent, socks, mug.

Why use two stages at all? Compute recall@6 for retrieval and recall@3 for the page. What is the best recall@3 any ranker could reach here? For each missed product, which stage is to blame?

Show hints (3)
  1. "WHY TWO STAGES"

    The ranking model is accurate but slow for each product it scores. What would it cost to run it on all 2 million products for every page view?

  2. "RECALL AT K"

    Recall@k is the relevant products found in the top k, divided by all relevant products. Here there are 4 relevant products, whatever k is.

  3. "THE CEILING"

    The ranker can only reorder the 6 candidates. Which relevant products are among them? A product that is not in that list can never reach the page.

Show checked explanation

Scoring every product with the heavy model is too slow, so cheap retrieval cuts millions down to a short list, and the ranker orders only that list. Retrieval's recall caps the whole system. Here retrieval found 2 of 4 (recall@6 = 0.5), the page shows 1 of 4 (0.25), and a perfect ranker could reach 0.5.

"SOLUTION · PYTHON"
relevant = {"tent", "stove", "lamp", "mat"}  # what this shopper would buy
retrieved = ["socks", "tent", "mug", "lamp", "cap", "rope"]  # stage 1, top 6
shown = ["tent", "socks", "mug"]  # stage 2, the ranker's top 3


def recall_at_k(ranked, k):
    return len(relevant & set(ranked[:k])) / len(relevant)


best = min(len(relevant & set(retrieved)), 3) / len(relevant)
print("retrieval recall@6:", recall_at_k(retrieved, 6))
print("page recall@3:", recall_at_k(shown, 3))
print("best possible recall@3:", best)
for item in sorted(relevant - set(shown)):
    stage = "ranker" if item in retrieved else "retrieval"
    print(f"{item}: missed by {stage}")
"OUTPUT"
retrieval recall@6: 0.5
page recall@3: 0.25
best possible recall@3: 0.5
lamp: missed by ranker
mat: missed by retrieval
stove: missed by retrieval

Code run on CPython 3.12 against the example above. Checked by an editor other than the author.

Walkthrough

Two stages split the work. Retrieval looks at the whole catalogue, so it must be cheap for each product: an embedding search or simple rules. The ranker only sees the short list, so it can afford rich features and a slow model for each product.

stagelistrelevant foundrecall
retrieval, top 6socks, tent, mug, lamp, cap, ropetent, lamp2 / 4 = 0.5
page, top 3tent, socks, mugtent1 / 4 = 0.25

The best ranker would put tent and lamp in the top 3. That gives 2 / 4 = 0.5, the same as retrieval's recall. No ranker can do better, because stove and mat are not in its input.

So you can blame each miss on one stage:

  • stove and mat: retrieval. They never reached the ranker.
  • lamp: the ranker. It had lamp and still put socks and mug above it.

This tells you what to fix. If retrieval recall is low, a better ranker can't help. If it is high but the page is poor, work on the ranker.

In general, the page's recall can't beat retrieval's recall. With more relevant candidates than slots, the cap is lower still: slots ÷ all relevant products.

Common mistakes

  • Computing 2 found ÷ 6 returned = 0.33 and calling it recall. That is precision@6. Recall divides by all relevant products: 4.
  • Spending months on the ranker while retrieval recall is low. Here even a perfect ranker loses stove and mat.
  • Scoring the ranker only on the candidates it received and calling that the quality of the whole system. It hides every product retrieval never returned.
  • Making the candidate list much longer to raise recall without checking latency. The ranker's cost grows with every candidate it scores.

Follow-up questions

  • "How do you raise retrieval recall without making it much slower?" Merge several cheap sources: embedding search, items similar to recent views, and items popular in the shopper's region. Each catches products the others miss. Remove duplicates and send the merged list to the ranker. Measure recall@k of the merged list, not of each source.
  • "In production you don't know all relevant products. How do you measure retrieval recall?" Use logged outcomes as the relevant set, for example what each shopper bought next. Hide those events, run retrieval, and count how many appear in the top k. These labels come from products people already saw, so they favour what the old system showed.

What a strong answer covers

Explains the split by cost: cheap retrieval over the whole catalogue, expensive ranking over a short list. Computes recall@k with all relevant items in the denominator, shows that retrieval recall caps any ranker, and blames each miss on the stage that lost it.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from Inside the Machine Learning Interview (Peng Shao, 2023); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.