KatInterview

Indentation-Aware Code Folding Engine

Data Structure DesignModel answer
MediumModel answerData Structure Designsimulationmonotonic-stackdata-structure-design

Last updated

Build a class that collapses and restores indentation-defined code blocks, tracking nested fold state and remapping display line numbers after each operation.

Background

Text editors like VS Code let you fold away blocks of code — clicking a triangle next to a function header hides its body until you unfold it. Your job is to build the data structure behind that feature.

Initialization

Your editor is created with a flat list of source-code lines. Block structure is entirely determined by leading-space indentation, the way Python works:

  • Every line's indentation is a multiple of 4 spaces.
  • A line H is a block header when the line directly following it is indented more deeply than H.
  • The body of header H is the unbroken sequence of lines after H that are all indented more deeply than H. The body ends at the first line whose indentation is no greater than H's, or at the end of the file.
  • Bodies can contain their own headers, creating nested folds.

Displaying the buffer

Implement a render() method that returns the list of currently visible lines, one entry per visible line, in this format:

<display_number> <marker><original line text>
  • display_number counts visible lines from 1; hidden lines are skipped entirely and do not occupy a number.
  • marker is + (plus then a space) for any block header, and the empty string for every other line.
  • The original line text includes its leading spaces.

Fold operations

Both operations below take a display number — the number shown in the most recent render() output, not a position in the original file.

shrink(d) — Fold the block whose header is currently shown at display number d. The header line stays visible; every line in its body is hidden. If d does not point to a header, or that header's block is already folded, do nothing.

expand(d) — Unfold the block whose header is currently shown at display number d. Lines that were hidden solely because of this fold become visible again. However, any line that is hidden because a nested header inside this block was previously folded remains hidden — each header tracks its own fold state independently.

A line is visible if and only if it is not contained in the body of any currently-folded header.

Example

Editor initialized with these five lines (4-space indent shown explicitly):

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 shrink(2), render() returns:

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

The two body lines are hidden and b = 10 is renumbered to display 3. Calling expand(2) after that restores the original five-line output.

What to implement

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⁴; each line is at most 200 characters.
  • Indentation is spaces only, always a multiple of 4. The first line has indentation 0. No line is indented more than 4 spaces deeper than the one before it.
  • At most 10⁴ total calls across shrink, expand, and render.
  • The display number passed to shrink or expand is always valid for the most recent render() output.

Hints

Hint 1

Separate what never changes (block structure) from what changes on every fold (the collapsed/expanded flag per header). Compute each header's block-end index once at construction time using a monotonic stack — the same "next smaller or equal element" pattern used in problems like daily temperatures.

Hint 2

To enumerate visible lines you don't need a hidden flag on every line. A single forward scan is enough: emit the current line, then if it is collapsed jump directly to its block-end index (skipping the whole body in O(1)), otherwise advance by one. Nested collapsed headers inside an expanded block are encountered naturally and trigger their own jumps.

Hint 3

Display numbers are relative to the current visible list and become stale after every fold. Never cache a mapping from original file index to display number across calls. Instead, resolve display number d by taking the (d−1)-th element of the list produced by the visibility scan on each operation.