FastPrepFind All Word Occurrences in Text
Problems/Find All Word Occurrences in Text

Find All Word Occurrences in Text

Sentry logoSentry● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report from March 2023 on a Sentry senior backend engineer interview describes a live coding round with the hiring manager. It was a word search over a longer text, where you find each spot that one of several given words shows up. The candidate rated it easy.

The report does not say what the answer should look like. This version returns the start and end index of every match, sorted. It counts matches that overlap or sit inside a longer word. The character set and the limits are our choices too.

Our version is about 75% the same as the reported question.

You are given a block of text text and a list of words words. Find every place where a word from words appears in text.

Report each match as a pair [i, j], where text[i..j] (from index i to index j, both included) is exactly equal to one of the words. Indices start at 0.

Every match counts, even when two matches overlap or when a word appears inside a longer word. Because words contain no spaces, a match never crosses a space.

Return all pairs sorted by i, and pairs with the same i sorted by j. If no word appears, return an empty list.

Function

indexPairs(text: String, words: String[]) → int[][]

Examples

Example 1

text = "the cat sat on the mat"words = ["the","at"]return = [[0,2],[5,6],[9,10],[15,17],[20,21]]

The word "the" appears at indices 0 to 2 and 15 to 17. The word "at" appears inside "cat" (5 to 6), "sat" (9 to 10) and "mat" (20 to 21). Sorted by start index, the answer is [[0,2],[5,6],[9,10],[15,17],[20,21]].

Example 2

text = "ababa"words = ["aba","ab"]return = [[0,1],[0,2],[2,3],[2,4]]

"ab" appears at 0 to 1 and 2 to 3. "aba" appears at 0 to 2 and again at 2 to 4, so the two matches share index 2. Pairs with the same start are ordered by end, so [0,1] comes before [0,2]. The answer is [[0,1],[0,2],[2,3],[2,4]].

Example 3

text = "hello world"words = ["xyz"]return = []

"xyz" does not appear anywhere in the text, so the answer is an empty list.

Constraints

  • 1 <= text.length <= 1000
  • text contains only lowercase English letters and spaces.
  • 1 <= words.length <= 20
  • 1 <= words[i].length <= 50
  • words[i] contains only lowercase English letters.
  • All strings in words are distinct.

More Sentry interview questions

  • Word Wrap with Even JustificationOA · PHONE SCREEN · Seen Dec 2025
  • Five-Minute Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2022
  • Minimum Meeting RoomsPHONE SCREEN · Seen Feb 2022
  • Wildcard Bad-Word FilterOA · Seen Aug 2022

Similar questions at other companies

  • Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
  • Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Parse and Sort Integer DataLegora · PHONE SCREEN
  • Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
  • Remove Later Anagrams From a ListBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions
  • Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
  • Group AnagramsWorkday · PHONE SCREEN
  • Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions

Interview reports that mention this question

  • Sentry Experienced Engineer Interviews: 14 Experiences

Sentry interview experiences

  • Sentry Software Engineer Interview Questions: 18 Experiences
  • Sentry SWE Intern OA and Technical Interview: 16 Experiences
  • Sentry SWE New Grad OA and Technical Screen: 12 Experiences
All Sentry interview questionsSee Sentry hiring insights
Saved
class Solution {
    public int[][] indexPairs(String text, String[] words) {
        // Write your code here.
    }
}
Checking account…