Design File Structure for Sparse File

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-07-08

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:


β€’Efficient storage and retrieval of sparse data,
β€’Logical addressing (offset-based reads/writes),
β€’Extensibility for persistence or caching layers.


βœ… Functional Requirements


Create Sparse File

β€’Initialize a file representation with a given name and logical size.

Write(offset, data)

β€’Write data starting from a byte offset.
β€’Merge overlapping or adjacent blocks.
β€’Update the file’s logical size if needed.

Read(offset, length)

β€’Return data from specified offset and length.
β€’Unwritten regions must return zero bytes (or '\0').

Truncate(newSize)

β€’Reduce or increase logical file size.
β€’Remove or create sparse gaps as required.

GetMetadata()

β€’Return metadata like file name, logical size, number of blocks, and total used storage.

ListBlocks()

β€’Return the list of stored (non-zero) blocks with start offset and size.


βš™οΈ 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


β€’Use an ordered map (TreeMap / SortedDict) for block management.
β€’Key β†’ block start offset
β€’Value β†’ Block(startOffset, data)
β€’On write:
β€’Find affected blocks.
β€’Merge adjacent/overlapping regions.
β€’Insert new block(s).
β€’On read:
β€’Retrieve relevant blocks using binary search or map lookup.
β€’Combine stored data with zero-filled gaps.


🧾 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 bytes

Output:

1 2Read(995, 10) β†’ "\0\0\0\0\0World"


βš™οΈ Design Patterns to Consider

β€’Composite Pattern – For treating multiple blocks as one file.
β€’Flyweight Pattern – For reusing zero regions logically.
β€’Strategy Pattern – To plug in storage backends (memory, file, cloud).
β€’Iterator Pattern – For sequential reads/writes.


πŸ“ˆ Possible Extensions

β€’Add compression per block.
β€’Add persistence layer for file storage.
β€’Add block caching or memory-mapped support.
β€’Implement concurrency for multi-threaded reads/writes.


πŸ’¬ 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.


Solution

Comments