FastPrepRich-Text Segment Normalization and Bold Toggle

Rich-Text Segment Normalization and Bold Toggle

Scale AI logoScale AI● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Each string in segments has format BI:text. B and I are 0 or 1 for the bold and italic flags; text is nonempty lowercase text.

  1. Normalize the input by merging adjacent segments with identical bold and italic flags.
  2. Using character positions in the concatenated text, toggle the bold flag for the half-open range [start,end). Preserve text order and italic flags, split segments at range boundaries, and normalize adjacent equal-style segments again.

Return the normalized encoded segments.

Function

toggleBoldRange(segments: String[], start: int, end: int) → String[]

Examples

Example 1

segments = ["10:ab","10:cd","00:ef"]start = 1end = 5return = ["10:a","00:bcd","10:e","00:f"]

The first two segments merge to abcd. Toggling indices 1 through 4 unbolds bcd and bolds e; a and f keep their styles.

Example 2

segments = ["00:abc","01:def"]start = 0end = 6return = ["10:abc","11:def"]

The entire text becomes bold while each italic flag is preserved.

Example 3

segments = ["10:a","10:b"]start = 1end = 1return = ["10:ab"]

The empty range changes nothing, and normalization merges the two segments.

Constraints

  • 1 <= segments.length <= 100000.
  • Total text length is at most 300000.
  • Every segment matches [01][01]:[a-z]+.
  • 0 <= start <= end <= total text length.

More Scale AI problems

See Scale AI hiring insights
public String[] toggleBoldRange(String[] segments, int start, int end) {
    // write your code here
}
segments["10:ab","10:cd","00:ef"]
start1
end5
expected["10:a", "00:bcd", "10:e", "00:f"]
Checking account…