Highlighted Context Windows
Updated
Problem statement
You are given an array of words, a non-empty string target, and a whole number context that is never negative. A word matches when it contains target at least once. Matching is case-sensitive, and target is plain text, not a pattern.
Each matching word makes a window: that word plus up to context whole words on each side. A window stops at the start and end of the array.
Merge windows that overlap or touch. Two windows touch when one ends right before the other starts. Return the merged windows from left to right, each as its words in their original order. Inside every returned word, wrap each occurrence of target with the marker **. Find occurrences from left to right, without overlap. Leave words with no occurrence unchanged.
Function
highlightContextWindows(words: String[], target: String, context: int) → String[][]Examples
Example 1
words = ["alpha","catapult","beta","scatter","gamma"]target = "cat"context = 1return = [["alpha","**cat**apult","beta","s**cat**ter","gamma"]]Both catapult and scatter contain cat. Each window takes its matching word plus the nearest word on each side. So the windows cover indices 0 through 2 and 2 through 4. They share index 2, so they merge into a window that holds every word. The answer is that merged window: alpha, **cat**apult, beta, s**cat**ter, gamma.
Example 2
words = ["aaaa","x","aa"]target = "aa"context = 0return = [["**aa****aa**"],["**aa**"]]Here context is zero, so each window holds only its matching word. Both aaaa and the last word contain aa. The word x sits between them, so their windows do not touch and stay separate. In aaaa, the search finds aa at the start and again right after it, so that word becomes **aa****aa**. The last word becomes **aa**. The answer is these two windows, from left to right.
Constraints
0 <= words.length <= 100000.0 <= words[i].length <= 1000and the total number of characters is at most300000.1 <= target.length <= 1000.0 <= context <= words.length + 1000.- The literal marker
**does not occur in any input word or intarget.
More Anduril interview questions
- Add Word and Suffix SearchONSITE INTERVIEW · Seen Aug 2026
- Binary Tree Preorder TraversalONSITE INTERVIEW · Seen Aug 2026
- Operator-First Array CalculatorONSITE INTERVIEW · Seen Aug 2026
- Daily TemperaturesONSITE INTERVIEW · Seen Jun 2024
- Pacific Atlantic Water FlowPHONE SCREEN · Seen Aug 2026
- Choose the Front Football TeamPHONE SCREEN · Seen May 2026
- Maximum Visible DronesPHONE SCREEN · Seen Jun 2024
- Build a Sorted Circular Singly Linked ListPHONE SCREEN · Seen Feb 2024
- Sparse Vector Dot ProductPHONE SCREEN · Seen Apr 2021
Similar questions at other companies
- Cipher Cover Interval EditingDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Earliest Time to Schedule a MeetingMicrosoft · OAMicrosoft 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
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions