KatInterview

Building a Search Engine for Social Media Posts

System DesignModel answer
MediumModel answerSystem Designindexingdistributed-systemssearch

Last updated

Design a search system that indexes post content, handles include/exclude queries, and ranks matches by term frequency across distributed infrastructure.

You're building the search infrastructure for a social media platform. Users need to search through post content using text queries and receive relevant results ordered by match quality.

Your system must handle:

  1. Efficiently locating posts containing specific terms without scanning the entire corpus
  2. Query logic that requires certain terms while forbidding others (e.g., must contain "coffee" but exclude "decaf")
  3. Ranking matched posts by how frequently query terms appear in each document

What You Need to Design

Describe the complete architecture including:

  • How newly created or modified posts become searchable
  • How search queries are processed and answered
  • Deep technical details on indexing, query execution with exclusions, and frequency-based ranking

Questions to Consider

Scale and performance:

  • What are your assumptions about corpus size, write rate, read rate?
  • What latency target are you designing for?
  • How quickly must new posts appear in search results?

Search semantics:

  • How do you tokenize and normalize text (case, punctuation, stemming)?
  • Are query terms individual words or can they be phrases?
  • With multiple inclusion terms, do you require all or any?
  • Can a query contain only exclusion terms with no required terms?

Access control:

  • Must search respect post visibility and user permissions?

Part 1: Term-to-Document Index

Explain how you locate all posts containing a given term without full corpus scans. Cover index construction, maintenance as posts change, storage requirements, and horizontal partitioning.

Part 2: Exclusion Logic

Support queries with both required and forbidden terms. Address evaluation order, performance when excluded terms are common, correct pagination behavior, and edge cases like unknown terms.

Part 3: Frequency-Based Ranking

Return results ordered by relevance measured through term frequency. Explain score computation for multi-term queries, efficient top-K selection without full sorts, result merging across machines, pagination consistency, and limitations of raw frequency counts.

Hints

Hint 1

Build an inverted index mapping each term to the list of documents containing it, with frequency metadata.

Hint 2

Evaluate required terms first to get candidates, then filter out exclusions before pagination to avoid incorrect page sizes.

Hint 3

Use heap-based top-K selection per partition, then merge sorted streams; store frequency counts in the index for scoring without document retrieval.