Design File Structure for Sparse File
Problem Statement
Design the internal file structure and supporting classes for a Sparse File System, where large files with uninitialized (zero-filled) regions are represented efficiently in memory or on disk without wasting storage space.
Your goal is to design the classes, relationships, and data flow that make this representation possible, including read/write/truncate operations and how file blocks are stored, merged, or retrieved.
πΉ Context
In traditional file systems, even unwritten portions of a file consume disk space (e.g., 1 GB file with only 4 KB of data still occupies 1 GB).
A sparse file optimizes this by storing only the non-zero data blocks and logically representing unwritten areas as zeros.
You are asked to design the file structure and class model that supports:
β Functional Requirements
Create Sparse File
Write(offset, data)
Read(offset, length)
Truncate(newSize)
GetMetadata()
ListBlocks()
βοΈ Non-Functional Requirements
RequirementDescription
Space Efficiency
Unwritten regions should consume no storage.
Performance
Fast read/write lookups (O(log n) ideally).
Scalability
Should handle large logical files (multi-GB) efficiently.
Extensibility
Easy to extend to disk persistence or compression later.
Integrity
Ensure correct data retrieval even with overlapping writes.
π§© Example
1
2Write(0, "ABC")
3Write(100, "XYZ")
4Read(0, 105) β "ABC\0\0...XYZ"Logical file size: 103 bytes
Physically stored: only 6 bytes ("ABC" + "XYZ")
π§± Core Entities & Responsibilities
Class / InterfaceDescription
SparseFile
Represents the logical sparse file, manages metadata and block operations.
Block
Represents a contiguous region of non-zero data (startOffset, data).
BlockIndex / BlockMap
Data structure (e.g., ordered map) to store and search blocks by offset.
FileSystem (optional)
Manages multiple SparseFiles, handles persistence or file metadata.
βοΈ Data Structure Design
π§Ύ Sample Class Diagram
1
2βββββββββββββββββββββββ
3β SparseFile β
4βββββββββββββββββββββββ
5β - name: String β
6β - size: int β
7β - blocks: TreeMap<int, Block> β
8βββββββββββββββββββββββ
9β + read(offset, len) β
10β + write(offset, data)β
11β + truncate(size) β
12β + listBlocks() β
13βββββββββββββ¬ββββββββββ
14 β
15 βΌ
16βββββββββββββββββββββββ
17β Block β
18βββββββββββββββββββββββ
19β - startOffset: int β
20β - data: byte[] β
21βββββββββββββββββββββββ
22β + getEnd(): int β
23βββββββββββββββββββββββπ§ Example Flow
Input:
1
2file = SparseFile("data.bin")
3file.write(0, "Hello")
4file.write(1000, "World")
5file.read(995, 10)Internal State:
1
2Blocks:
30: "Hello"
41000: "World"
5
6Logical Size = 1005 bytesOutput:
1
2Read(995, 10) β "\0\0\0\0\0World"βοΈ Design Patterns to Consider
π Possible Extensions
π¬ Interview Prompt Summary
Design the file structure for a Sparse File system.
Your solution should define the classes and data structures required to store large logical files efficiently by maintaining only non-zero regions.
Demonstrate how reads, writes, and truncates are handled while minimizing space usage and maintaining logical continuity.