Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-045

Phrase Query Check

Problem

Search a positional index for documents where terms occur as a consecutive phrase in the given order. A match at position p requires terms[k] at position p + k in the same document.

Return the sorted integer document ids with at least one match.

Hint

For every start position of the first term, check the next term at exactly p + 1, then p + 2, and so on.

Test cases

case 1
in: [{"new":{"0":[0],"1":[1],"2":[1]},"york":{"0":[1],"1":[0],"2":[2]},"city":{"0":[2],"1":[2],"2":[0]}},["new","york"]]
expected: [0,2]
case 2
in: [{"new":{"0":[0],"1":[1],"2":[1]},"york":{"0":[1],"1":[0],"2":[2]},"city":{"0":[2],"1":[2],"2":[0]}},["york","city"]]
expected: [0]
case 3
in: [{"new":{"0":[0],"1":[1],"2":[1]},"york":{"0":[1],"1":[0],"2":[2]},"city":{"0":[2],"1":[2],"2":[0]}},["city","new"]]
expected: [2]
case 4
in: [{"new":{"0":[0],"1":[1],"2":[1]},"york":{"0":[1],"1":[0],"2":[2]},"city":{"0":[2],"1":[2],"2":[0]}},["hello"]]
expected: []
Starter codePython
def phrase_query(index, terms):
    # Your code here
    pass

4 test cases · run your code against them in the editor