Bold a Visible HTML Substring
Updated
Problem statement
You are given a well-formed HTML fragment html and two indexes, start and end. The indexes count positions in the visible text, starting from zero, and end is not included. Return the two offsets [rawStart,rawEnd] in the raw HTML where <b> and </b> should be inserted, so that exactly that visible substring becomes bold.
Both offsets point into the original fragment, before either tag is inserted. Tags add no visible characters. The selected range is not empty and sits entirely inside one text node. The fragment has no entities and no existing b tags. All characters are ASCII.
Function
boldVisibleSubstring(html: String, start: int, end: int) → int[]Examples
Example 1
html = "<p>Hello <em>world</em>!</p>"start = 6end = 11return = [13,18]Tags add no visible characters, so the visible text is Hello world!. Indexes 6 through 10 cover world. In the raw string, world starts right after the opening em tag and ends right before the closing em tag. Those two raw offsets are the answer.
Example 2
html = "Hi <span>there</span>"start = 0end = 2return = [0,2]The first two visible characters are Hi, and no tag comes before them. So the raw offsets are the same as the visible indexes, and the later span tag is left alone.
Constraints
1 <= html.length <= 100000- The fragment is well formed and every tag closes with
> 0 <= start < end <= visibleTextLength- The selected visible range lies inside one text node
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions