Word Search Across Documents
Updated
Problem statement
Given an array of text documents and a query word, return the zero-based indices of all documents that contain the query as a complete word.
Words are lowercase English-letter tokens separated by one space. Matching is exact: a query does not match part of a longer token. Include each matching document index once, in increasing order.
Follow-up: one reusable index
Preprocess the document collection once, then use the same data structure for repeated single-word and phrase queries. Explain how storing each word's document IDs and positions supports both operations without rebuilding or maintaining a separate index for each query form.
Documents may have explicit document numbers. Here, the array index is the document number; in a service, retain the external document number in each posting. Your function only does the single-word lookup. For phrase practice, try the companion sequential phrase-search problem.
Function
findDocumentsWithWord(documents: String[], word: String) → int[]Examples
Example 1
documents = ["red blue green","bluebird flies","green blue"]word = "blue"return = [0,2]Documents 0 and 2 contain token blue. Token bluebird is not an exact match.
Example 2
documents = ["alpha beta","gamma delta","alpha"]word = "omega"return = []No document contains the token omega.
Constraints
1 <= documents.length <= 1000.1 <= documents[i].length <= 1000.1 <= word.length <= 50.- Every document contains lowercase English words separated by exactly one space, with no leading or trailing space.
wordcontains only lowercase English letters.
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
- Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 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
- Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Get Best PriceONSITE INTERVIEW · Seen Apr 2025
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN
- Longest Common Parent DirectoryGoogle · ONSITE INTERVIEWGoogle interview questions