Implement a Memory Based Sparse File Class
Problem Statement
Design and implement a SparseFile class that simulates a file in memory, efficiently storing only non-zero data blocks.
This class should support basic file-like operations — read, write, seek, and getSize — while minimizing memory usage by avoiding storage of uninitialized (zero-filled) regions.
🔹 Problem Context
In a typical file, uninitialized or zero-filled regions still occupy physical storage.
However, in systems like Linux or cloud storage (e.g., AWS S3 sparse objects), files can be sparse — meaning only written regions consume memory/disk, and unwritten parts are treated as zeros.
Your task is to model this behavior in-memory using an object-oriented approach.
✅ Functional Requirements
🧱 Core Operations
Write(offset, data)
Read(offset, length)
GetSize()
Truncate(newSize)
Seek(offset) (optional)
⚙️ Non-Functional Requirements
RequirementDescription
Memory Efficiency
Should not store large zero-filled areas.
Performance
Reads/writes should be O(log N) or better for N sparse blocks.
Extensibility
Easy to modify for future persistence (e.g., disk-based version).
Integrity
Handle overlapping writes and invalid reads gracefully.
Thread Safety (Optional)
Can be extended to support concurrent reads/writes.
🧩 Example Usage
1
2file = SparseFile()
3
4file.write(0, "Hello")
5file.write(1000, "World")
6
7print(file.read(0, 10)) # Output: "Hello\0\0\0\0\0"
8print(file.read(995, 10)) # Output: "\0\0\0\0\0World"
9print(file.getSize()) # Output: 1005🧠 Explanation:
🧩 Core Classes and Responsibilities
Class / InterfaceDescription
SparseFile
The main class representing an in-memory sparse file. Handles read/write/truncate operations.
SparseBlock
Represents a contiguous non-zero data block with attributes like start offset and data.
BlockManager
Maintains an ordered structure (e.g., TreeMap or sorted list) of SparseBlocks for efficient lookup.
MemoryAllocator (Optional)
Manages memory allocation for blocks and merges adjacent ones.
⚙️ Data Structure Design
1
2{
3 start_offset: SparseBlock(start_offset, data)
4}🧱 Design Patterns to Consider
🧾 Class Diagram (Text Form)
1
2┌─────────────────────┐
3│ SparseFile │
4│ ─────────────────── │
5│ - blocks: Map<int, SparseBlock> │
6│ + write(offset, data) │
7│ + read(offset, length): str │
8│ + getSize(): int │
9│ + truncate(size) │
10└───────────┬────────────────────┘
11 │
12 ▼
13┌─────────────────────┐
14│ SparseBlock │
15│ ─────────────────── │
16│ - startOffset: int │
17│ - data: bytes │
18│ + getEndOffset(): int│
19└─────────────────────┘🧠 Algorithmic Details
📝 Write Operation
📖 Read Operation
✂️ Truncate Operation
📊 Example Execution
Write:
1
2write(0, "ABC")
3write(10, "XYZ")Internal Representation:
1
2blocks = {
3 0: SparseBlock(0, "ABC"),
4 10: SparseBlock(10, "XYZ")
5}Read(0, 15) → "ABC\0\0\0\0\0\0XYZ\0\0"
🔮 Extensions
💬 Interview Prompt Summary
Design and implement a SparseFile class that represents an in-memory file storing only non-zero data blocks efficiently.
The class should support file-like operations (read, write, truncate, getSize) while minimizing memory usage for sparse regions.
Emphasize data structure choices, merging logic, and extensibility.