Implement Layer History and Grid Counting
Task 1: Implement Layer History and Grid Counting
Implement a simplified layer editor for a design tool. The editor maintains an ordered list of layer IDs. Support the following operations:
addLayer(id, index): Insert a new layer at the given position.removeLayer(id): Remove an existing layer.moveLayer(id, newIndex): Move a layer to a new position.undo(): Revert the most recent committed edit.redo(): Re-apply the most recently undone edit.beginBatch()andendBatch(): All edits inside a batch must be treated as one atomic history entry.
Constraints for Task 1:
- Batches are not nested.
- If a new edit is applied after an
undo(), the redo history must be cleared. undo()andredo()must restore the exact previous layer ordering.
Task 2: Count Connected Land Regions
Given a 2D grid containing L for land and W for water, count how many connected land regions exist. Two land cells belong to the same region if they share a side (not diagonally).
Deliverables for Task 2:
- Write an algorithm.
- Analyze its time and space complexity.
AI interview prep
Core dilemma
Designing an efficient and robust undo/redo system for a mutable ordered list, balancing memory usage for history against the performance of state restoration, and ensuring atomic batch operations.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Layer History and Grid Counting
Task 1: Implement Layer History and Grid Counting
Implement a simplified layer editor for a design tool. The editor maintains an ordered list of layer IDs. Support the following operations:
addLayer(id, index): Insert a new layer at the given position.removeLayer(id): Remove an existing layer.moveLayer(id, newIndex): Move a layer to a new position.undo(): Revert the most recent committed edit.redo(): Re-apply the most recently undone edit.beginBatch()andendBatch(): All edits inside a batch must be treated as one atomic history entry.
Constraints for Task 1:
- Batches are not nested.
- If a new edit is applied after an
undo(), the redo history must be cleared. undo()andredo()must restore the exact previous layer ordering.
Task 2: Count Connected Land Regions
Given a 2D grid containing L for land and W for water, count how many connected land regions exist. Two land cells belong to the same region if they share a side (not diagonally).
Deliverables for Task 2:
- Write an algorithm.
- Analyze its time and space complexity.