PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

Implement a stateful autocomplete session that ranks matching historical sentences by frequency and lexicographic order. Process each keystroke, commit completed nonempty sentences, update future suggestions, and keep results independent of the initial input order.

  • hard
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Implement a Stateful Search Autocomplete Session

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

# Implement a Stateful Search Autocomplete Session Build an autocomplete engine from historical sentences and their usage counts. While a user types a session, return up to three historical sentences that begin with the current prefix. Rank candidates by descending usage count, breaking ties by lexicographically smaller sentence. The order of the initial `sentences` input must not affect the result. The character `#` ends the current sentence. Commit the nonempty accumulated text with one additional use, return an empty suggestion list for that character, and reset the prefix for a new session. ## Function Contract ```python def run_autocomplete( sentences: list[str], frequencies: list[int], keystrokes: str, ) -> list[list[str]]: ... ``` Return one suggestion list for each character in `keystrokes`. ## Constraints - `len(sentences) == len(frequencies)` - Initial sentences are distinct and nonempty. - Sentences and typed characters contain lowercase English letters and spaces; `#` appears only in `keystrokes`. - `1 <= frequencies[i] <= 10**6` - At most 500 initial sentences and 500 keystrokes are supplied. - If `#` is entered with an empty prefix, do not add an empty sentence. ## Example ```text sentences = ["island", "ironman", "i love you", "i love leetcode"] frequencies = [3, 2, 5, 2] keystrokes = "i a#i" ``` The function returns a list of suggestions after each character, including an empty list at `#`, and the newly committed sentence participates in later prefixes.

Quick Answer: Implement a stateful autocomplete session that ranks matching historical sentences by frequency and lexicographic order. Process each keystroke, commit completed nonempty sentences, update future suggestions, and keep results independent of the initial input order.

Implement run_autocomplete(sentences, frequencies, keystrokes). After each ordinary character, return up to three historical sentences beginning with the current prefix, ranked by descending count then lexicographically. # commits the nonempty prefix with one additional use, returns an empty suggestion list, and resets the session. Return one suggestion list per keystroke.

Constraints

  • len(sentences) == len(frequencies); initial sentences are distinct and nonempty.
  • Sentences and ordinary keystrokes contain only lowercase English letters and spaces; # appears only in keystrokes.
  • 1 <= frequencies[i] <= 1,000,000.
  • At most 500 initial sentences and 500 keystrokes are supplied.
  • Entering # with an empty prefix returns an empty list, resets the prefix, and does not store an empty sentence.

Examples

Input: ([], [], "a#")

Expected Output: [[], []]

Explanation: A new sentence has no suggestions before it is committed.

Input: (["a"], [2], "a#")

Expected Output: [["a"], []]

Explanation: An exact historical sentence is suggested, then committed.

Hints

  1. Keep sentence counts across # boundaries.
  2. Apply both ranking keys before taking three matches.
Last updated: Jul 18, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.

Related Coding Questions

  • Mark and Compact a Heap-Indexed Subtree - Pinterest (medium)
  • Collect Pins from Reachable Boards - Pinterest (medium)
  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)