KatInterview

Building a Query Completion Prediction System

Machine LearningModel answer
MediumModel answerMachine Learningml-system-designrankinginformation-retrieval

Last updated

Design an ML system that predicts and ranks query completions in real-time as users type search terms.

You need to design a machine learning system that provides query completion suggestions while users type into a search box. For instance, when someone types "ipho", the system should offer completions like "iphone 15" or "iphone charger".

Your design should address:

  • Business goals and metrics: What defines success? Consider response time, suggestion quality, user engagement, result variety, and content safety.
  • Data collection and training labels: Where does training data come from? How do you create labels while accounting for biases in user behavior?
  • System architecture: How do you structure candidate retrieval versus final ranking? What are the computational trade-offs?
  • Model inputs and algorithms: What signals inform predictions? How do you incorporate user history and context? Which algorithms fit the latency constraints?
  • Training infrastructure and evaluation: How do you build the offline pipeline? What offline metrics predict online success?
  • Production serving: How do you meet strict latency requirements? What caching and fallback strategies work?
  • Experimentation and monitoring: How do you run experiments safely? What can go wrong in production?
  • Special scenarios: How do you handle new queries that haven't been seen before, sudden spikes in interest for specific terms, and malicious or inappropriate content?

Discuss the full system end-to-end, making concrete technology choices and explaining the rationale behind each decision.

Hints

Hint 1

Think about the two-stage funnel: first quickly find hundreds of plausible completions, then carefully rank the best ones

Hint 2

User clicks are biased—suggestions at the top get more clicks regardless of quality. How do you correct for this in training?

Hint 3

Sub-100ms latency means aggressive caching and precomputation. What can you prepare offline versus what must happen per request?