Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-242

Piece Table Insert

Problem

Simulate the insert side of a piece table text buffer.

original is the immutable original text and every inserted string goes to an append-only add buffer. operations contains ["insert", pos, text] with 0-based positions. Pieces reference a source (0 original, 1 added), a start, and a length.

Return the materialized final text.

Hint

An insert at a piece boundary splits the piece and adds a new added-source piece between the halves.

Test cases

case 1
in: ["abc",[["insert",1,"XY"],["insert",0,"z"]]]
expected: "zaXYbc"
case 2
in: ["",[["insert",0,"hi"]]]
expected: "hi"
case 3
in: ["abc",[["insert",3,"!"]]]
expected: "abc!"
case 4
in: ["abc",[["insert",1,""]]]
expected: "abc"
Starter codePython
def piece_table_insert(original, operations):
    # Your code here
    pass

4 test cases · run your code against them in the editor