PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/J.P. Morgan

Design an autocomplete suggestions API

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of string-oriented data structures (e.g., prefix trees/tries), ranked retrieval and dynamic updates, testing skills in efficient prefix search, frequency-based ordering, and lexicographical tie-breaking.

  • easy
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

Design an autocomplete suggestions API

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Onsite

## Problem: Autocomplete suggestions (Trie) You are building an autocomplete feature. ### Input - A dictionary of `n` unique words `words[i]` (lowercase a–z). - An integer array `freq[i]` where `freq[i]` is the usage count of `words[i]`. - Then `q` queries. Each query provides: - a `prefix` string - an integer `k` (number of suggestions requested) ### Output For each query, return up to `k` suggested completions: - A completion is any word that starts with `prefix`. - Rank by **higher frequency first**. - Break ties by **lexicographical order**. ### Follow-up Support updates: - Operation `add(word, delta)` increases that word’s frequency by `delta` (word may be new). - Queries after updates must reflect the new frequencies. ### Constraints (reasonable interview assumptions) - `1 ≤ n, q ≤ 2e5` - `1 ≤ |word|, |prefix| ≤ 50` - Total characters across all words/queries can be large; solutions should be better than scanning all words per query. Explain what data structure you would use (e.g., Trie) and implement the required operations.

Quick Answer: This question evaluates understanding of string-oriented data structures (e.g., prefix trees/tries), ranked retrieval and dynamic updates, testing skills in efficient prefix search, frequency-based ordering, and lexicographical tie-breaking.

Related Interview Questions

  • Implement Integer Square Root - J.P. Morgan (medium)
  • Implement KNN from Scratch - J.P. Morgan (medium)
  • Minimize Operations to Balance Shipments - J.P. Morgan (medium)
  • Solve scheduling and collision problems - J.P. Morgan (medium)
  • Group strings that are anagrams - J.P. Morgan (easy)
J.P. Morgan logo
J.P. Morgan
Jan 22, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
5
0
Loading...

Problem: Autocomplete suggestions (Trie)

You are building an autocomplete feature.

Input

  • A dictionary of n unique words words[i] (lowercase a–z).
  • An integer array freq[i] where freq[i] is the usage count of words[i] .
  • Then q queries. Each query provides:
    • a prefix string
    • an integer k (number of suggestions requested)

Output

For each query, return up to k suggested completions:

  • A completion is any word that starts with prefix .
  • Rank by higher frequency first .
  • Break ties by lexicographical order .

Follow-up

Support updates:

  • Operation add(word, delta) increases that word’s frequency by delta (word may be new).
  • Queries after updates must reflect the new frequencies.

Constraints (reasonable interview assumptions)

  • 1 ≤ n, q ≤ 2e5
  • 1 ≤ |word|, |prefix| ≤ 50
  • Total characters across all words/queries can be large; solutions should be better than scanning all words per query.

Explain what data structure you would use (e.g., Trie) and implement the required operations.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More J.P. Morgan•More Software Engineer•J.P. Morgan Software Engineer•J.P. Morgan Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.