Sequential Phrase Search Across Documents
Updated
Problem statement
Given an array of text documents and a query phrase, return the zero-based indices of every matching document, once each in increasing order. Match complete lowercase words exactly; a word prefix is not a match.
Query forms
- A plain query such as
alpha betais an ordered phrase: its words must appear in that order in the document, but they do not have to be adjacent. Each query occurrence consumes a later document occurrence, soalpha alphaneeds two occurrences. - A query such as
alpha AND betarequires every listed word to occur somewhere in the document, in any order. Repeated operands do not require extra occurrences. - A query such as
alpha OR betarequires at least one listed word to occur.
A one-word plain query is an exact-word lookup. Every Boolean query uses exactly one operator kind, with at least two lowercase-word operands and a single space around uppercase AND or OR. Mixed operators, parentheses, negation and malformed queries are outside this exercise. Plain queries and documents use one space between lowercase English words and have no leading or trailing spaces.
In short, phrase words must appear in order, gaps between them are allowed, and AND and OR queries are part of the task.
Follow-up: reuse preprocessing
Build one reusable positional index for the document collection and use that same store for both single-word and phrase queries. A posting associates a word with a document number and its sorted token positions; do not preprocess separate document collections for the two operations. The existing one-word query form already serves word lookup.
Documents may carry explicit document numbers. Here, each document's number is its zero-based position in the array; a reusable service can retain external document numbers in the same postings. This follow-up leaves all query forms and matching rules above unchanged.
Function
findDocumentsWithPhrase(documents: String[], phrase: String) → int[]Examples
Example 1
documents = ["distributed systems need careful testing","careful distributed systems testing","distributed reliable systems"]phrase = "distributed systems"return = [0,1,2]Every document has "distributed" followed later by "systems", and gaps are allowed. All three documents match, so the answer is the indices zero, one and two.
Example 2
documents = ["alpha beta gamma","beta alpha gamma","alpha gamma beta"]phrase = "alpha beta"return = [0,2]Document zero has "alpha" and then "beta". Document one has "beta" before "alpha", so it does not match. Document two has "alpha", then a gap, then "beta", so it matches. The answer is the indices zero and two.
Example 3
documents = ["alpha beta gamma","beta alpha gamma","alpha gamma beta"]phrase = "gamma AND beta"return = [0,1,2]This is an AND query, so the order does not matter. Every document has both "gamma" and "beta". The answer is the indices zero, one and two.
Example 4
documents = ["alpha","beta","delta alpha","alphabet"]phrase = "alpha OR delta"return = [0,2]This is an OR query. Document zero has "alpha" and document two has both words, so both match. Document one has neither word. Document three has only "alphabet", which is not the whole word "alpha". The answer is the indices zero and two.
Constraints
1 <= documents.length <= 1000.1 <= documents[i].length <= 1000.1 <= phrase.length <= 200.- Documents contain lowercase English words separated by exactly one space.
- The query is one of the valid plain, uniform-AND or uniform-OR forms described above.
More Confluent Onsite Interview questions
- Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
- Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
- Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
- Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
- Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Get Best PriceONSITE INTERVIEW · Seen Apr 2025
Similar questions at other companies
- Travelers per Consecutive City TripletUber · ONSITE INTERVIEWUber interview questions
- Ordered IP FirewallDatabricks · PHONE SCREENDatabricks interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions