cswork / libraryOpen practice workspace

Interview questions · Enigma

Search Book Titles

This question evaluates proficiency in string processing, tokenization, case-insensitive matching, anagram detection, and algorithmic design for efficient search and deduplication.

You are given a list of book titles and a user query string. Return all titles that are relevant to the query.

A title is considered relevant if any token in the title matches the query under at least one of these rules:

  1. Exact match: the token equals the query, ignoring case.
  2. Autocomplete match: the token starts with the query, ignoring case.
  3. Reordered-letter match: the token is an anagram of the query, ignoring case. For example, culb matches Club because they contain the same letters.

Requirements:

  • Tokenize each title into words by splitting on spaces.
  • Matching is case-insensitive.
  • Return each matching title at most once.
  • Preserve the original input order of titles.

Example:

  • Titles: ["The Luck of Club"]
  • Query: "Luck" -> return ["The Luck of Club"]
  • Query: "luc" -> return ["The Luck of Club"] because luc is a prefix of Luck
  • Query: "culb" -> return ["The Luck of Club"] because culb is an anagram of Club

Design and implement an efficient function for this search problem. Discuss the trade-offs between preprocessing the titles and answering queries quickly.

Community answers

Nothing here yet.

Join in on CSWork →