Hi there, thank you for taking the time to interview with us today. This question is abstracted from our codebase, and we hope you will enjoy solving this problem.
The question might be harder than your initial reaction. Please read everything in this instruction carefully.
Good luck!
Rich text in Slab is represented in a JSON-based format for consistency and specificity. A simplified version for a nested list might look something like this:
[
{ "text": "One", "indent": 0, "type": "ordered" },
{ "text": "Two", "indent": 0, "type": "ordered" },
{ "text": "Alpha", "indent": 1, "type": "bullet" },
{ "text": "Beta", "indent": 1, "type": "bullet" },
{ "text": "I", "indent": 2, "type": "ordered" },
{ "text": "II", "indent": 2, "type": "ordered" },
{ "text": "Three", "indent": 0, "type": "ordered" }
]
Which the front end will render to something like:
- One
- Two • Alpha • Beta i. I ii. II
- Three
It is sometimes necessary to convert this JSON representation into HTML for exporting, emails, or pasting into other applications.
Your task is to write a program that handles this conversion. The above output would look like this:
<ol>
<li>One</li>
<li>Two
<ul>
<li>Alpha</li>
<li>Beta
<ol>
<li>I</li>
<li>II</li>
</ol>
</li>
</ul>
</li>
<li>Three</li>
</ol>
Submission
Your main function should be called deltaToHtml. It should accept an array/list as the input, and returns a string as the output. Input
You do not need to parse a JSON string. You can assume the input is already an array. In order, each item in the array represents an item in the list. Each item has 3 attributes:
text: the pure text content to be displayed in the item
indent: item's indent level. You can assume indent will at most increase by 1 from one item to its next item.
type: what type of list is the item in, the possible values are:
bullet: <ul> type list
ordered: <ol> type list
Output
The HTML presentation of the input.
Notes:
In our examples, we included indentations for readability. Your submission does not need to output any white space.
According to the HTML spec, a nested ol/ul element needs to be contained in a li element. For example, both of the following examples are incorrect:
<-- Invalid HTML syntax -->
<ol>
<li>One</li>
<li>Two</li>
<ul>
<li>Alpha</li>
<li>Beta</li>
</ul>
<li>Three</li>
</ol>
<-- Valid HTML syntax, but does NOT represent an expected nested list -->
<ol>
<li>One</li>
<li>Two</li>
<li>
<ul>
<li>Alpha</li>
<li>Beta</li>
</ul>
</li>
<li>Three</li>
</ol>
Additional Info
- You may choose any language to implement your program by changing the language setting in CoderPad.
- You have limited time. We do care about code quality, but correctness comes first.
- If you have extra time, you could use it on testing, improving readability, writing docs, etc.
- http://htmlformatter.com can be helpful for reading the output.
- If you have any questions, please email [email protected].