PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Jane Street
  • Coding & Algorithms
  • Software Engineer

Collapsible Code Editor: Brace Matching and Toggle

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Collapsible Code Editor: Brace Matching and Toggle You are building a feature for a code editor. The editor holds source code written in a curly-brace language (like C++ or Java) that uses `{` to open a block and `}` to close it. The document is represented as a list of lines, one string per line. To simplify parsing, you may assume: - A brace, if present on a line, appears **only as the last character of that line**. At most one brace appears per line. - The input is **always valid**: braces are balanced and properly nested (every `{` has a matching `}` that closes after it, and pairs never partially overlap). - Lines may also contain no brace at all (plain code or text). Implement the following in two parts. ## Part 1 — Match the braces Write a function that takes the list of lines and returns the matching between every opening brace and its corresponding closing brace, keyed by line index. ``` matchBraces(lines: List[str]) -> Dict[int, int] ``` - The returned mapping has one entry per brace pair. - The **key** is the 0-based line index of a line ending in `{`. - The **value** is the 0-based line index of the matching line ending in `}`. ### Example ``` Input lines: 0: "class A {" 1: " void f() {" 2: " doWork();" 3: " }" 4: " int x;" 5: "}" Output: {0: 5, 1: 3} ``` Line 0's `{` matches line 5's `}`; line 1's `{` matches line 3's `}`. ## Part 2 — Toggle collapse / expand Add an interactive collapse feature. The editor starts with every block **expanded** (fully shown). Support two operations: ``` toggle(openLineIndex: int) -> None # flip the collapsed state of the brace pair whose '{' is on openLineIndex getText() -> List[str] # return the lines currently displayed ``` Collapsing semantics: - `toggle(i)` flips the collapsed/expanded state of the single brace pair whose opening `{` is on line `i`. (You may assume `i` is the index of a line that ends in `{`.) - When a brace pair is **collapsed**, the entire range from its `{` line through its matching `}` line is shown as one single line whose text is the original opening line with `" ... }"` appended. - For the example above, collapsing the pair on line 1 would display line 1 as `" void f() { ... }"`, and lines 2 and 3 would not be shown at all. - When a brace pair is **expanded** (the default), its opening and closing lines and the content between them are shown normally, subject to the collapse state of any nested pairs. Independence of nesting — this is the key requirement: - Each brace pair has its **own independent** collapsed/expanded state. - Collapsing or expanding an outer pair must **not** change the stored state of any pair nested inside it. - Concretely: if an inner pair is collapsed and you then collapse the outer pair, the outer pair hides everything (so you only see the outer one-line summary). If you later expand the outer pair, the inner pair is revealed **still in its collapsed state** — not re-expanded. `getText()` must reflect the current collapsed/expanded state of all pairs, rendering only the lines that are visible from the outside in. ### Example of independent state Starting from the example document, suppose the following calls happen in order: ``` toggle(1) # collapse the inner pair (lines 1-3) getText() -> 0: "class A {" 1: " void f() { ... }" 2: " int x;" # this is original line 4 3: "}" # this is original line 5 toggle(0) # collapse the outer pair (lines 0-5) getText() -> 0: "class A { ... }" toggle(0) # expand the outer pair again getText() -> 0: "class A {" 1: " void f() { ... }" # inner pair is STILL collapsed 2: " int x;" 3: "}" ``` ## Requirements - `matchBraces` should run in a single pass over the lines. - `getText()` should produce the correct visible rendering for any combination of collapsed/expanded pairs, respecting nesting independence. - Index handling must be precise: when a pair is collapsed, every line strictly between its `{` and `}` (inclusive of the `}` line) is suppressed and replaced by the single summary line.

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

You are building a feature for a code editor that holds source code in a curly-brace language. The document is a list of lines (one string per line). A brace, if present on a line, appears only as the last character of that line, and at most one brace appears per line. The input is always valid: braces are balanced and properly nested. Lines may also contain no brace at all. Write `matchBraces(lines)` that returns the matching between every opening brace and its corresponding closing brace, keyed by line index. The key is the 0-based line index of a line ending in `{`; the value is the 0-based line index of the matching line ending in `}`. There is one entry per brace pair. Example: ``` 0: "class A {" 1: " void f() {" 2: " doWork();" 3: " }" 4: " int x;" 5: "}" ``` returns `{0: 5, 1: 3}` — line 0's `{` matches line 5's `}`, and line 1's `{` matches line 3's `}`. Requirement: run in a single pass over the lines.

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

  1. A stack of open-brace line indices is the classic tool: push the index when a line ends in '{'.
  2. When a line ends in '}', pop the stack — that popped index is the matching '{' line for this '}'.
  3. 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

Building on Part 1, add an interactive collapse feature. The editor starts with every block expanded. `toggle(openLineIndex)` flips the collapsed/expanded state of the single brace pair whose opening `{` is on that line. `getText()` returns the lines currently displayed. When a pair is collapsed, the whole range from its `{` line through its matching `}` line is shown as one line: the original opening line with `" ... }"` appended; every line strictly after the `{` up to and including the `}` is suppressed. When expanded (default), its lines are shown normally, subject to the collapse state of nested pairs. Key requirement — independent nesting: each pair has its own collapsed state. Collapsing/expanding an outer pair must NOT change the stored state of any pair nested inside it. If an inner pair is collapsed and you then collapse and later expand the outer pair, the inner pair is revealed still collapsed. To make this stateful behavior testable as a single function, implement `renderAfterToggles(lines, toggles)`: `lines` is the document, and `toggles` is the ordered list of `openLineIndex` values passed to `toggle()`. Apply the toggles in order, then return the final `getText()` result as a list of strings. Example: starting from the 6-line class document, `toggles = [1, 0, 0]` (collapse inner, collapse outer, expand outer) returns `["class A {", " void f() { ... }", " int x;", "}"]` — the inner pair is still collapsed after the outer is re-expanded.

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

  1. Reuse the Part 1 stack to build the open->close index map first.
  2. Keep one boolean per pair for collapsed/expanded; a toggle simply flips that pair's boolean — never touch nested pairs' booleans.
  3. 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.
Last updated: Jun 24, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Circular Buffer - Jane Street (medium)
  • Optimize trade PnL table updates - Jane Street (hard)
  • Transform sparse time-code stream to dense rows - Jane Street (easy)
  • Sort trade executions into a canonical order - Jane Street (easy)
  • Solve queue switch and coin change puzzles - Jane Street (medium)