KatInterview

Batch Word and In-Order Phrase Lookup Over Text Documents

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designinverted-indexhash-mapbinary-search

Last updated

Build an inverted index with token positions to answer whole-word and in-order phrase queries over documents, handling repeated words and punctuation.

You are given a list of text documents. Document ids are their indices in the list (0, 1, 2, ...). You are also given a batch of queries, and you must answer each one against the documents.

Input

  • documents: a list of strings.
  • operations: a list of strings. Each entry is "word" or "phrase".
  • arguments: a list of lists of strings. arguments[i] holds the arguments for operations[i]. The query text is arguments[i][0], or the empty string if arguments[i] is empty.

Output

Return a list with one entry per query, in the same order as operations. Each entry is a list of document ids sorted in ascending order, with no duplicates.

Tokenization

Apply this rule to documents and to query text alike:

  1. Convert the text to a case-insensitive form with Python's str.casefold().
  2. Split it into tokens, where a token is a maximal run of Unicode letters and digits. Every other character, including punctuation, whitespace, hyphens and the underscore _, is a separator.

For example, "State-of-the-art" becomes ["state", "of", "the", "art"], and "To be, or not to be." becomes ["to", "be", "or", "not", "to", "be"].

Query types

"word": tokenize the query text. If it yields exactly one token, return the ids of the documents whose token list contains that token (a whole-word match, never a substring match). If it yields zero tokens or more than one token, the answer is an empty list.

"phrase": tokenize the query text into t1, t2, ..., tk. A document matches if its token list contains positions p1 < p2 < ... < pk such that the token at pj equals tj for every j. The words must appear in the same order as in the phrase, but other tokens may appear between them, so they need not be adjacent. A repeated word in the phrase must be matched by a different, later occurrence in the document. If the phrase yields no tokens, the answer is an empty list.

Any other operation name: the answer is an empty list.

Constraints

  • 0 <= len(documents) <= 10^4, and the total number of tokens across all documents is at most about 10^6.
  • len(operations) == len(arguments), with at most 10^4 queries.
  • Documents and queries may contain arbitrary Unicode text, including empty strings.
  • Documents are fixed for the whole batch, so preparing an index once is allowed and encouraged.
  • Each answer list must be in ascending order of document id.

Examples

Example 1

Input: run_text_queries(['The quick brown fox.', 'A QUICK fox jumps.'], ['word', 'phrase'], [['quick'], ['quick fox']])

Output: [[0, 1], [0, 1]]

Example 2

Input: run_text_queries(['To be, or not to be.'], ['word', 'phrase'], [['be'], ['to be or not to be']])

Output: [[0], [0]]

Example 3

Input: run_text_queries(['state-of-the-art systems', 'State of the art design', 'artistic systems'], ['word', 'phrase', 'phrase'], [['art'], ['state art'], ['state of the art']])

Output: [[0, 1], [0, 1], [0, 1]]

Hints

Hint 1

Tokenize once per document and build an inverted index that maps each token to the documents containing it, plus the sorted positions of that token inside each document.

Hint 2

For a phrase, first intersect the document sets of its distinct tokens to get candidates. Then verify each candidate separately.

Hint 3

To verify in-order matching, walk through the phrase and, for each token, find the first occurrence strictly after the previous matched position (binary search over the stored positions). This handles repeated words correctly.