FastPrepSequential Phrase Search Across Documents
Problems/Sequential Phrase Search Across Documents

Sequential Phrase Search Across Documents

Confluent logoConfluent● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports describe searching documents for words and phrases. The earlier report gives a sequence of words. The current report allows words of a phrase that are not next to each other, and adds AND and OR lookups. Our version replaces the earlier rule of words that must be next to each other with words that must appear in order, and keeps exact word tokens. We chose one rule for the operators, how repeated words work, the order of the index, and the limits. Our version is about 90% the same as the combined reports.

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 beta is 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, so alpha alpha needs two occurrences.
  • A query such as alpha AND beta requires 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 beta requires 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

Interview reports that mention this question

  • Confluent SWE OA, Coding and Design Interviews: 19 Experiences
  • Confluent Coding and System Design Questions: 33 Questions

Confluent interview experiences

  • Confluent Older Concurrency Rounds: 12 Experiences (Part 2 of 2)
  • Confluent Concurrency Interviews: 28 Experiences (Part 1 of 2)
  • Confluent File Manipulation Round (tail -n): 19 Experiences
All Confluent interview questionsSee Confluent hiring insights
Saved
class Solution {
    public int[] findDocumentsWithPhrase(String[] documents, String phrase) {
        // write your code here
    }
}
Checking account…