#22MockSearchEngine keyword retrievalEasy

MockSearchEngine keyword retrieval

Background

MockSearchEngine is a deterministic search tool: it scans the query for each keyword in its document database and returns all docs for every keyword found. If nothing matches, it returns a single "not found" message.

for keyword, docs in db.items():
    if keyword in query.lower():
        results.extend(docs)
return results or [f"No documents found for: '{query}'"]

Problem statement

Implement mock_search(query, db) reproducing this retrieval.

Input

  • query — the search string.
  • db — dict mapping a keyword to a list of doc strings.

Output

Returns a list of docs: every doc whose keyword appears (case-insensitively) in query, in db order; or ["No documents found for: '<query>'"] if none match.

Examples

Example 1 — one keyword hits

Input:  query = "AlphaCode benchmark", db = {"alphacode": ["d1", "d2"]}
Output: ["d1", "d2"]

Example 2 — no match

Input:  query = "transformers", db = {"alphacode": ["d1"]}
Output: ["No documents found for: 'transformers'"]

Constraints

  • Case-insensitive keyword-in-query check.
  • Extend results with all docs of each matching keyword, in db insertion order.
  • No matches → ["No documents found for: '<query>'"] (original-case query).

Notes

  • Returning all docs for every matched keyword is why a two-entity query can accidentally fetch both topics — and why one blind search is brittle.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: one keyword hits
  • Sample: two keywords both present -> both doc sets in db order
  • Reference: case-insensitive keyword match