Design a Search Ranking Algorithm Using Inverted Index
Requirements: Given products each with a name, tags, and description, design a search algorithm that returns results ranked by how well they match a query term (highest matching frequency first).
Design: Build an inverted index mapping each token (word, after tokenizing/normalizing name+tags+description) to the list of product IDs containing it, along with a term frequency count per product for ranking. At query time, tokenize the query, look up each token's posting list, and score candidate products by aggregate matching frequency (e.g., TF, or TF-IDF to down-weight common tokens across the catalog) across all matched fields, returning results sorted by score descending. In practice this maps directly onto Elasticsearch/Lucene's inverted-index + BM25/TF-IDF scoring, and the interview typically wants you to explain how Elasticsearch would implement this out of the box (analyzers for tokenization, per-field boosting for name > tags > description) rather than hand-roll the entire index.