Code Editor Folding
Problem statement
Implement brace matching and code folding for an immutable list of source lines. Braces are balanced and properly nested; every brace is structural. Each line has at most one brace, as its last non-whitespace character. A closing-brace line contains only indentation and }. All indices refer to the original source, starting at zero.
Every block starts expanded and has an independent folding flag. Toggling a parent changes only that flag, preserving all descendant flags. A hidden descendant can also be toggled by its original opening-line index.
For this exercise, process an ordered operation list and return one string-array row per operation:
MATCH: return each opening/closing line pair asopening:closing, ordered by opening index. This is the portable representation of the brace-matching map.TOGGLE i: switch the flag for opening linei, even if hidden. Return an empty row.GET: return a fresh list of visible source lines in original order. For each visible folded block, keep its opening line through the{, append... }, and omit subsequent lines through its matching closing line. Preserve every other visible line exactly.
The source may be empty. The original source is never modified. As an extension, explain how to map a current display-line index back to an original opening line before toggling; the judged operations continue to use original indices.
Function
codeEditorOperations(lines: String[], operations: String[]) → String[][]Examples
Example 1
lines = ["def outer() {"," before();"," if (ready) {"," work();"," }"," after();","}","done();"]operations = ["MATCH","TOGGLE 2","GET","TOGGLE 0","GET","TOGGLE 0","GET"]return = [["0:6","2:4"],[],["def outer() {"," before();"," if (ready) { ... }"," after();","}","done();"],[],["def outer() { ... }","done();"],[],["def outer() {"," before();"," if (ready) { ... }"," after();","}","done();"]]Nested state survives parent folding and expansion.
Example 2
lines = []operations = ["MATCH","GET"]return = [[],[]]Empty source supports brace matching and an empty view.
Example 3
lines = ["{","}"]operations = ["GET","TOGGLE 0","GET","TOGGLE 0","GET"]return = [["{","}"],[],["{ ... }"],[],["{","}"]]A two-line empty block folds and expands.
Constraints
0 <= lines.length <= 500; each line has at most 100 characters, each a printable ASCII character (codes 32 through 126) or a horizontal tab (code 9). No other whitespace or line-break characters occur.1 <= operations.length <= 500; each operation has one documented form.- Every toggle index identifies an original opening-brace line.
- Across all returned rows, the total number of characters is at most 20000.