Scale AI OA Interview Coding Question: RichText Formatting

19 Views
No Comments

RichText Formatting

When displaying text at Scale, we want to be able to store and display richtext: text that has formatting like bold, italic, etc. For this interview, we will only consider the formatting options of bold and italic.

The way that we store richtext will be as a JSON array of objects that represent text segments with formatting options. The definitions for this JSON type is:

type RichTextSegment = {
  text: string;
  bold: boolean;
  italic: boolean;
}

type RichText = RichTextSegment[];

Part 1: Validate and Fix

Our richtext format should follow the following rule: any two consecutive segments of text in the RichText object should not have redundant segments. Segments are redundant if they have the same formatting combination of bold and italic.

Implement a function fix(richtext) that takes invalid constructed richtext objects that have redundantly formatted segments and converts them into valid richtext objects by concatenating the text of those segments together. For example, the invalid text above is converted into the valid text above.

fix(invalidText: RichText) -> validText: RichText

Examples:

  • invalidText = [
  • {"bold": true, "italic": false, "text": "One two "},
  • {"bold": true, "italic": false, "text": "three four"},
  • ]

Valid result:

[{"bold": true, "italic": false, "text": "One two three four"}
]

This problem asks you to normalize a rich text array by merging adjacent segments that share the same formatting flags, <code>bold</code> and <code>italic</code>. A simple linear pass is enough: keep the last segment in the output, append text when the next segment has the same style, and otherwise start a new segment. The main skills tested are array traversal, state tracking, and string concatenation.

END
 0