Collapsible Code Editor: Brace Matching and Toggle
Company: Jane Street
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question tests stack-based parsing and stateful data structure design through a two-part brace-matching and collapsible editor problem. It evaluates practical algorithm implementation — specifically single-pass traversal and nested state management — skills commonly assessed in coding interviews for software engineering roles.
Collapsible Code Editor — Part 1: Match the Braces
Constraints
- Each line is a string; a brace, if present, is the last character.
- At most one brace per line.
- Input is always valid: braces balanced and properly nested.
- Lines may contain no brace at all.
Examples
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"],)
Expected Output: {0: 5, 1: 3}
Explanation: Outer pair (line 0) matches line 5; inner pair (line 1) matches line 3.
Input: ([],)
Expected Output: {}
Explanation: Empty document has no braces, so the mapping is empty.
Input: (["int x;", "return 0;"],)
Expected Output: {}
Explanation: Lines with no braces produce no entries.
Input: (["a {", "}"],)
Expected Output: {0: 1}
Explanation: A single pair: line 0's '{' matches line 1's '}'.
Input: (["a {", "b {", "c {", "}", "}", "}"],)
Expected Output: {2: 3, 1: 4, 0: 5}
Explanation: Triple nesting: innermost (2->3), middle (1->4), outermost (0->5).
Input: (["a {", "}", "b {", "}"],)
Expected Output: {0: 1, 2: 3}
Explanation: Two sibling pairs at the same level.
Hints
- A stack of open-brace line indices is the classic tool: push the index when a line ends in '{'.
- When a line ends in '}', pop the stack — that popped index is the matching '{' line for this '}'.
- Because the input is always valid, you never pop an empty stack and the stack is empty at the end.
Collapsible Code Editor — Part 2: Toggle Collapse / Expand
Constraints
- toggle(i) is only ever called with i being a line that ends in '{'.
- A collapsed pair's summary is the opening line plus the literal ' ... }'.
- When collapsed, every line strictly after '{' up to and including the matching '}' is suppressed.
- Each pair's collapsed state is independent; outer toggles never alter inner state.
Examples
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [])
Expected Output: ["class A {", " void f() {", " doWork();", " }", " int x;", "}"]
Explanation: No toggles: everything expanded, full document shown.
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [1])
Expected Output: ["class A {", " void f() { ... }", " int x;", "}"]
Explanation: Collapsing the inner pair (line 1) hides lines 2-3 and shows the inner summary.
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [1, 0])
Expected Output: ["class A { ... }"]
Explanation: After collapsing inner then outer, the outer summary hides everything else.
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [1, 0, 0])
Expected Output: ["class A {", " void f() { ... }", " int x;", "}"]
Explanation: Independent state: re-expanding the outer pair reveals the inner pair STILL collapsed.
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [0])
Expected Output: ["class A { ... }"]
Explanation: Collapsing the outer pair alone shows only its one-line summary.
Input: (["class A {", " void f() {", " doWork();", " }", " int x;", "}"], [1, 1])
Expected Output: ["class A {", " void f() {", " doWork();", " }", " int x;", "}"]
Explanation: Toggling the same pair twice restores it to expanded.
Input: (["a {", "b {", "c {", "}", "}", "}"], [1])
Expected Output: ["a {", "b { ... }", "}"]
Explanation: Collapsing a middle pair in triple nesting hides the innermost lines too.
Input: (["a {", "}", "b {", "}"], [0, 2])
Expected Output: ["a { ... }", "b { ... }"]
Explanation: Two sibling pairs each collapsed independently.
Hints
- Reuse the Part 1 stack to build the open->close index map first.
- Keep one boolean per pair for collapsed/expanded; a toggle simply flips that pair's boolean — never touch nested pairs' booleans.
- Render with a single index pointer scanning outside-in: if the current line opens a collapsed pair, emit the summary and jump the pointer to one past its matching '}'; otherwise emit the line and advance by one. This naturally respects nested collapse states.