Apply Overlapping HTML Formatting Ranges
Reported by candidates from Benchling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Benchling question, reported in February 2019, is treating each format as its own open and close tag pair. Then crossing ranges produce broken HTML and the sample output never matches. You're turning bold, italic and underline ranges into valid nested tags, with escaping on top. It's a sweep over character positions, not string splicing. If you blank mid-assessment, StealthCoder is the invisible safety net that reads the problem on screen and hands you a working structure. Know the trick before you need it.
The problem
Apply formatting ranges to text. Each entry in formats is type:start:end, where type is bold, italic, or underline and [start,end) is a half-open character range. Emit valid HTML using b, i, and u tags. When ranges cross, close and reopen tags as needed. Nest active tags in bold, italic, underline order. For this exercise, assume the input is literal text. Escape each original &, <, and > as &, <, and >, respectively, while inserting the formatting tags. Escaping does not change the original range indices. Function applyHtmlFormats(text: String, formats: String[]) → String Examples Example 1 text = "ABCDE" formats = ["bold:0:2","italic:1:3"] return = "<b>A<i>B</i></b><i>C</i>DE" The crossing ranges require italic to close before bold and reopen afterward. Constraints 1 <= text.length <= 10000. 0 <= formats.length <= 10000. Every range satisfies 0 <= start < end <= text.length. For this exercise, assume text contains ASCII characters and is literal text, not existing HTML; range indices refer to the original characters before escaping.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking in ranges and think per character. Build a difference array for each of the three types, using +1 at start and -1 at end, so each index knows which formats are active. Walk the text once. At each index, compare the active set to the previous one. If the active set changes, close every open tag that needs to close, then reopen the ones that should stay active. Fixed nesting order (b, i, u) makes this deterministic. Keep a stack of open tags. When a tag deep in the stack must close, pop down to it, then reopen the survivors. The common pitfall is escaping: &, < and > expand the output, but indices refer to original characters, so escape while emitting and never before indexing. Also close every remaining tag at the end. StealthCoder is your hedge if the stack logic slips live, but this is a clean O(n + m) sweep once you see it.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Apply Overlapping HTML Formatting Ranges cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Benchling's OA.
Benchling reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Apply Overlapping HTML Formatting Ranges FAQ
What's the core trick for this Benchling OA problem?+
Convert ranges to per-character active sets with difference arrays, then sweep once. Whenever the active set changes, close tags down the stack to the one that ended, then reopen the ones still active. Fixed bold, italic, underline ordering keeps output deterministic.
How hard is it really?+
Medium. The algorithm is simple, but the tag close and reopen logic is easy to get wrong. The sample with bold:0:2 and italic:1:3 is your test. If your output matches it exactly, you've probably handled crossing correctly.
How should I handle escaping of &, < and >?+
Escape at emit time, one original character at a time. Indices refer to the original text, so never escape the whole string first. Each character gets its escaped form appended after any tag changes for that index.
What edge cases should I test?+
Test zero formats, duplicate identical ranges, ranges covering the whole text, ranges ending at text.length, and three types overlapping at once. Also check that all tags are closed at the end of the string and that duplicates don't double-open tags.
How do I prepare in 48 hours?+
Write the sweep from scratch twice. Practice the difference array for counts, then the stack-based close and reopen. Run the sample by hand. Skip anything fancy. With up to 10000 characters and 10000 formats, a linear sweep is plenty fast.