FastPrepBold a Visible HTML Substring

Bold a Visible HTML Substring

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a well-formed HTML fragment html and zero-based half-open indexes start and end into its visible text, return the two raw HTML offsets [rawStart,rawEnd] where a caller should insert <b> and </b> so exactly that visible substring becomes bold.

Offsets refer to the original fragment before either insertion. Tags contribute zero visible characters. The selected range is nonempty and lies wholly inside one text node, the fragment contains no entities or existing b tags, and 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]

HTML tags have zero visible width, so indexes 6 through 10 select world.

Example 2

html = "Hi <span>there</span>"start = 0end = 2return = [0,2]

The first two visible characters are wrapped without changing the later tag.

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 problems

See Microsoft hiring insights
public int[] boldVisibleSubstring(String html, int start, int end) {
  // Write your code here.
}
html"<p>Hello <em>world</em>!</p>"
start6
end11
expected[13,18]
Checking account…