Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← System Design Interview Playbook

Interview Framework

  • The 6-Step Design Framework

10 Case Studies

  • Design a URL Shortener
  • Design Twitter / X
  • Design WhatsApp
  • Design Netflix
  • Design a Rate Limiter
  • Design a Search Autocomplete
  • Design a Distributed Cache
  • Design a Notification Service
  • Design Uber / Ride Sharing
Chaturmind
← System Design Interview Playbook

Interview Framework

  • The 6-Step Design Framework

10 Case Studies

  • Design a URL Shortener
  • Design Twitter / X
  • Design WhatsApp
  • Design Netflix
  • Design a Rate Limiter
  • Design a Search Autocomplete
  • Design a Distributed Cache
  • Design a Notification Service
  • Design Uber / Ride Sharing
HomeLearnSystem DesignSystem Design Interview PlaybookDesign Cases
✓ FreeAdvanced· 13 min read

Design: Search Autocomplete

Design Google-scale search autocomplete with trie, top-k suggestions, prefix matching, and caching.

Published April 25, 2025


Design: Search Autocomplete

Requirements

  • Return top 5 suggestions as user types each character
  • Response time < 100ms
  • Suggestions ranked by search frequency
  • 5B searches/day, 10K queries/second during peak

Data Structure: Trie

A prefix tree (trie) enables O(k) prefix lookup where k = query length.

         root
        /    \
       a      b
      / \      \
    ap   ar    be
    |    |      |
  app   art   best

Each node stores the top K search suggestions for that prefix — precomputed.

Algorithm

class TrieNode {
    Map<Character, TrieNode> children = new HashMap<>();
    List<String> topK = new ArrayList<>(); // top 5 suggestions for this prefix
}

class Autocomplete {
    private TrieNode root = new TrieNode();

    public List<String> getSuggestions(String prefix) {
        TrieNode node = root;
        for (char c : prefix.toCharArray()) {
            if (!node.children.containsKey(c)) return List.of();
            node = node.children.get(c);
        }
        return node.topK; // pre-cached top K for this prefix
    }

    // Rebuild trie periodically from frequency data
    void buildTrie(Map<String, Long> queryFrequencies) {
        for (Map.Entry<String, Long> entry : queryFrequencies.entrySet()) {
            insert(entry.getKey(), entry.getValue());
        }
    }
}

System Architecture

Data Collection:
  User searches → Kafka → Aggregation Service → Query Frequency Store (Cassandra)
                   every 1 hour:
                        ↓
                 [Trie Builder Job] → serialized trie → S3
                        ↓ (every 1 hour)
                 [Autocomplete Servers] load new trie into memory

Query Path:
  User types "app" → [API Gateway] → [Autocomplete Server (trie in RAM)] → [Redis Cache] → top 5

Caching Strategy

80/20 rule: 20% of prefixes account for 80% of traffic
→ Cache top prefixes in Redis

// Cache key: "ac:{prefix}"
// TTL: 1 hour (matches trie rebuild frequency)

Personalization

Base suggestions: global top-K (from trie)
Personalized: blend with user's search history
  e.g., "apple" → global top = [apple, apple store, applebee's]
                 → for a developer: [apple, apple developer, apple swift]

Trie Scale Challenge

  • English: 26 chars, max query = 25 chars
  • Top queries: ~1 billion unique terms
  • Full trie in memory: ~10GB per server → feasible!
  • Or: shard by first character (a-f on server 1, etc.)

Interview Tips

  1. Pre-computing top-K at each trie node avoids expensive traversal at query time.
  2. Trie updates are expensive — rebuild offline (batched) rather than updating on every search.
  3. For mobile: send results after 3+ characters typed to reduce server load.

Previous

Design a Rate Limiter

Next

Design a Distributed Cache

AI Tutor

Lesson: Design: Search Autocomplete

Quick actions

AI responses can be inaccurate. Verify critical information.