Design a File Filtering System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-04

Problem Statement

Design the Low-Level Design (LLD) for a File Filtering System — a component that scans and filters files based on configurable criteria such as file type, size, date, or content.


Such systems are commonly used in:


•Antivirus and malware scanners
•Email attachment filters
•Cloud storage cleanup utilities (e.g., Google Drive “Large Files” filter)
•Data ingestion pipelines that must process only relevant files


The system should be modular, extensible, and efficient at filtering large numbers of files.


🎯 Goal


Design a File Filtering System that can:


•Accept a collection of files.
•Apply one or more filtering rules.
•Return or process only files that satisfy all or specific filter conditions.
•Support easy addition of new filter types (e.g., by file extension, size, creation date, or content keyword).


✅ Functional Requirements


1. Input Handling


•The system should take as input a list of File objects.
•Each File has properties like:
•name
•size
•type (extension)
•createdDate / modifiedDate
•content (optional)


2. Filtering Rules


•Filters can be applied based on different criteria such as:
•File Type (.txt, .pdf, .jpg, etc.)
•File Size (greater than, less than, between)
•File Creation/Modification Date
•File Content (e.g., contains a keyword like "CONFIDENTIAL")


3. Combinable Filters


•Users should be able to combine multiple filters using logical operators:
•AND (match all conditions)
•OR (match any condition)
•NOT (exclude specific filters)


Example:

Select all .txt files larger than 5MB that contain the keyword “report”.


4. Extensibility


•System should allow new filter types to be added easily without modifying core logic.
•e.g., “OwnerFilter” or “ReadOnlyFileFilter”


5. Output


•The system should return:
•List of filtered files
•(Optional) Summary report of total files scanned, filtered out, etc.


⚙️ Non-Functional Requirements


•Scalable for thousands of files.
•Extensible — new filters can be added easily.
•Testable — each filter can be unit tested independently.
•Readable and maintainable class design.


🧱 Key Classes to Design


ClassResponsibility


File


Represents metadata and attributes of a file.


FileFilter (interface)


Defines a standard method boolean matches(File file).


TypeFilter


Filters files by file extension/type.


SizeFilter


Filters files based on size range.


DateFilter


Filters files by creation/modification date.


ContentFilter


Filters files based on keyword search.


CompositeFilter


Combines multiple filters (AND, OR, NOT).


FileFilteringEngine


Core class that applies filters on a file list and returns matches.


🧩 Suggested Class Relationships

1 2File 3 ├── name 4 ├── size 5 ├── type 6 ├── createdDate 7 ├── content 8 9FileFilter (interface) 10 ├── boolean matches(File file) 11 12TypeFilter implements FileFilter 13 ├── matches(File file) 14 15SizeFilter implements FileFilter 16 ├── matches(File file) 17 18DateFilter implements FileFilter 19 ├── matches(File file) 20 21ContentFilter implements FileFilter 22 ├── matches(File file) 23 24CompositeFilter implements FileFilter 25 ├── List<FileFilter> filters 26 ├── FilterType (AND, OR, NOT) 27 ├── matches(File file) 28 29FileFilteringEngine 30 ├── applyFilters(List<File>, FileFilter) 31 ├── returns filtered list


🔄 Example Flow


🧾 Input


List of files:


NameTypeSize (KB)CreatedContent


report1.txt


txt

300

2024-01-15

“Monthly sales report”

data.pdf

pdf

1500

2024-03-10

“Confidential”

image.png

png

5000

2023-12-25

“Binary data”

report2.txt

txt

6000

2024-02-01

“Quarterly report”


Filters


•TypeFilter: .txt
•SizeFilter: size > 5000 KB


CompositeFilter (AND)


•Only .txt files larger than 5MB


🧮 Output


•[report2.txt]


🧠 Design Patterns to Use


•Strategy Pattern: Each filter is a separate strategy implementing FileFilter.
•Composite Pattern: Combine filters logically using AND/OR/NOT.
•Factory Pattern (optional): For dynamic filter creation from configuration.


⚡ Possible Extensions


•Add regex-based filtering for file names.
•Add config-based filters (read from JSON/YAML).
•Add asynchronous filtering for large file sets.
•Integrate with cloud storage APIs (AWS S3, GCS).
•Add logging and metrics (e.g., files filtered, total processed).


✅ Final Summary:


Design a File Filtering System (LLD) that:


•Filters files based on multiple configurable criteria.
•Combines filters logically (AND, OR, NOT).
•Uses interfaces and design patterns to support OCP (Open/Closed Principle).
•Is modular, extensible, and maintainable.


Solution

Comments