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
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.
The editor is initialized with a list of source-code lines. Indentation determines block structure, as in Python:
L
is a
block header
if the line immediately after it has strictly greater indentation than
L
.
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).
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.
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.
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.
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]: ...
1 <= number of lines <= 10^4
, each line at most 200 characters.
10^4
total
shrink
/
expand
/
render
calls.
d
that is valid for the most recent rendering (i.e.,
1 <= d <=
the current number of visible lines).