PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Jane Street

Code Editor with Block Shrink and Expand (Code Folding)

Last updated: Jul 2, 2026

Code Editor with Block Shrink and Expand (Code Folding)

Company: Jane Street

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement the core of a code editor (similar to VS Code) that supports **code folding**: collapsing ("shrink") and restoring ("expand") indentation-based blocks of code. ## Setup The editor is initialized with a list of source-code lines. Indentation determines block structure, as in Python: - Indentation is measured by the number of leading spaces, and every line's indentation is a multiple of 4 spaces. - A line `L` is a **block header** if the line immediately after it has strictly greater indentation than `L`. - The **block** owned by header `L` consists of all consecutive lines after `L` whose indentation is strictly greater than `L`'s indentation. The block ends at the first subsequent line whose indentation is less than or equal to `L`'s (or at the end of the file). - Blocks may be nested: a line inside a block may itself be a header of a smaller block. ## Rendering Implement a `render()` method that returns the currently **visible** lines, one string per line, formatted as: ``` <display_number> <marker><line content> ``` where: - `display_number` numbers the visible lines sequentially starting from 1 (hidden lines are skipped and do not consume a number). - `marker` is `+ ` if the line is a block header, and the empty string otherwise. - `line content` is the original text of the line, including its leading indentation. ## Operations Implement two operations. Both take a **display line number** — a number as shown in the most recent `render()` output, not the original file line number. - `shrink(d)`: if the visible line currently displayed at number `d` is a block header, mark it **collapsed**. A collapsed header stays visible, but every line inside its block becomes hidden. If the line at `d` is not a block header, or its block is already collapsed, this is a no-op. - `expand(d)`: if the visible line currently displayed at number `d` is a collapsed block header, mark it expanded again. Lines inside its block become visible **except** lines that are hidden because some nested header inside the block is itself still collapsed (each header keeps its own collapsed/expanded state). In general, a line is visible if and only if none of the headers whose blocks contain it are collapsed. ## Example The editor is initialized with these 5 lines: ``` a = 1 for i in range(10): print(i) print(i ** 2) b = 10 ``` `render()` returns: ``` 1 a = 1 2 + for i in range(10): 3 print(i) 4 print(i ** 2) 5 b = 10 ``` After calling `shrink(2)`, `render()` returns: ``` 1 a = 1 2 + for i in range(10): 3 b = 10 ``` Lines 3 and 4 of the original file (the body of the `for` loop) are hidden, and `b = 10` is renumbered to display number 3. Calling `expand(2)` afterwards restores the original output. ## Task Implement a class: ```python class CodeEditor: def __init__(self, lines: list[str]): ... def shrink(self, d: int) -> None: ... def expand(self, d: int) -> None: ... def render(self) -> list[str]: ... ``` ## Constraints - `1 <= number of lines <= 10^4`, each line at most 200 characters. - Indentation is spaces only, always a multiple of 4, and the first line has indentation 0. A line's indentation is at most one level (4 spaces) deeper than the line before it. - Up to `10^4` total `shrink` / `expand` / `render` calls. - Operations always receive a display number `d` that is valid for the most recent rendering (i.e., `1 <= d <=` the current number of visible lines).

Related Interview Questions

  • Collapsible Code Editor: Brace Matching and Toggle - Jane Street (medium)
  • 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)
|Home/Coding & Algorithms/Jane Street

Code Editor with Block Shrink and Expand (Code Folding)

Jane Street logo
Jane Street
Apr 24, 2026, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenCoding & Algorithms
0
0

Implement the core of a code editor (similar to VS Code) that supports code folding: collapsing ("shrink") and restoring ("expand") indentation-based blocks of code.

Setup

The editor is initialized with a list of source-code lines. Indentation determines block structure, as in Python:

  • Indentation is measured by the number of leading spaces, and every line's indentation is a multiple of 4 spaces.
  • A line L is a block header if the line immediately after it has strictly greater indentation than L .
  • The block owned by header L consists of all consecutive lines after L whose indentation is strictly greater than L 's indentation. The block ends at the first subsequent line whose indentation is less than or equal to L 's (or at the end of the file).
  • Blocks may be nested: a line inside a block may itself be a header of a smaller block.

Rendering

Implement a render() method that returns the currently visible lines, one string per line, formatted as:

<display_number> <marker><line content>

where:

  • display_number numbers the visible lines sequentially starting from 1 (hidden lines are skipped and do not consume a number).
  • marker is + if the line is a block header, and the empty string otherwise.
  • line content is the original text of the line, including its leading indentation.

Operations

Implement two operations. Both take a display line number — a number as shown in the most recent render() output, not the original file line number.

  • shrink(d) : if the visible line currently displayed at number d is a block header, mark it collapsed . A collapsed header stays visible, but every line inside its block becomes hidden. If the line at d is not a block header, or its block is already collapsed, this is a no-op.
  • expand(d) : if the visible line currently displayed at number d is a collapsed block header, mark it expanded again. Lines inside its block become visible except lines that are hidden because some nested header inside the block is itself still collapsed (each header keeps its own collapsed/expanded state).

In general, a line is visible if and only if none of the headers whose blocks contain it are collapsed.

Example

The editor is initialized with these 5 lines:

a = 1
for i in range(10):
    print(i)
    print(i ** 2)
b = 10

render() returns:

1 a = 1
2 + for i in range(10):
3     print(i)
4     print(i ** 2)
5 b = 10

After calling shrink(2), render() returns:

1 a = 1
2 + for i in range(10):
3 b = 10

Lines 3 and 4 of the original file (the body of the for loop) are hidden, and b = 10 is renumbered to display number 3. Calling expand(2) afterwards restores the original output.

Task

Implement a class:

class CodeEditor:
    def __init__(self, lines: list[str]): ...
    def shrink(self, d: int) -> None: ...
    def expand(self, d: int) -> None: ...
    def render(self) -> list[str]: ...

Constraints

  • 1 <= number of lines <= 10^4 , each line at most 200 characters.
  • Indentation is spaces only, always a multiple of 4, and the first line has indentation 0. A line's indentation is at most one level (4 spaces) deeper than the line before it.
  • Up to 10^4 total shrink / expand / render calls.
  • Operations always receive a display number d that is valid for the most recent rendering (i.e., 1 <= d <= the current number of visible lines).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Jane Street•More Machine Learning Engineer•Jane Street Machine Learning Engineer•Jane Street Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
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
  • AI Coding 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.