Skip to main content
medium

Search Suggestions System

A shop search box suggests products while a customer types. After each typed letter, offer up to three products that start with everything typed so far, taking the ones that come first alphabetically. Return one list per letter, and use an empty list when nothing matches. Limits: 1 <= products.length <= 100000, each product has 1 to 20 lowercase letters, and no product appears twice. The search word has 1 to 10000 lowercase letters. Think about: after you have handled one letter, what does the next letter tell you about which products can still match? Example: products = ["fjord", "fan", "gate", "finch", "figure", "fig", "fib"], searchWord = "fin" after "f": fan, fib and fig come first of the six that start with f after "fi": fib, fig and figure come first of the four that start with fi after "fin": finch is the only match Return [["fan","fib","fig"],["fib","fig","figure"],["finch"]].

You're building the search box that suggests products as someone types, offering the three alphabetically first matches after every letter. Interviewers love it because the obvious rescan on every keystroke feels fine on a small catalog and quietly melts on a big one. Watch the quiet cases too, like a letter that matches nothing and the letters after it. Expect up to 100,000 products and a search word of up to 10,000 letters.

Examples

You write

suggested_products(products: List[str], search_word: str)

  1. Example 1

    Input
    products = ["fjord","fan","gate","finch","figure","fig","fib"], searchWord = "fin"
    returns
    [["fan","fib","fig"],["fib","fig","figure"],["finch"]]
  2. Example 2

    Input
    products = ["tulip"], searchWord = "tulip"
    returns
    [["tulip"],["tulip"],["tulip"],["tulip"],["tulip"]]

Save this for your laptop

The editor and Run need a bigger screen. The link opens Search Suggestions System ready to code. It is free, and the editor needs no account.

The pattern behind it

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 49 Strings problems

Binary Search

I reach for it when

  • The input is sorted, or sorted and then rotated.
  • You want a boundary: the first or last place something is true.

Say this before you type. Say the yes-or-no question, and which side each answer throws away.

All 18 Binary Search problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

1Search Suggestions Systemmedium
Based onLeetCode

Get a note when something new lands

New company guides, new problems and big changes to Coco. Rare, and you can leave any time. By joining you agree to get these emails.