File Backup System
Problem
Design FileBackupSystem to apply batches of file writes to persistent current state and return the entries captured by a chosen backup policy.
Requirements
- Apply writes in order. Before producing output, each call applies its supplied
FileWriteobjects in list order. A later write to the same file replaces its earlier content. - Keep current state. Current file content persists across calls, including calls with another backup type or an empty writes list.
- Capture full state.
FULLreturns exactly one entry for every current file after applying the supplied writes. - Start with an empty baseline. The latest-full baseline is empty before the first
FULLcall. - Replace the full baseline. After applying its writes, each
FULLcall replaces the baseline with the complete resulting current state. - Preserve the full baseline.
DIFFERENTIALandLOGcalls leave the latest-full baseline unchanged. - Capture differences.
DIFFERENTIALreturns exactly one final entry for every current file whose content differs from the latest-full baseline after applying the supplied writes. - Capture the write log.
LOGreturns one entry for each supplied write in the same order, including repeated writes to one file.
API
FileWrite and BackupEntry are objects with string fields fileName and content.
| Signature | Returns | Behavior |
|---|---|---|
FileBackupSystem() | Not applicable | Creates a system with empty current state and an empty latest-full baseline. |
createBackup(backupType: string, writes: list<FileWrite>) | list<BackupEntry> | Applies the writes and returns entries for FULL, DIFFERENTIAL, or LOG. Only inputs satisfying the constraints are judged. |
Full and differential entries have no required order. Log entries preserve the supplied write order.
Examples
Start with a new system.
| Step | Operation | Result |
|---|---|---|
| 1 | createBackup("DIFFERENTIAL", [{fileName: "a.txt", content: "A1"}]) | [{fileName: "a.txt", content: "A1"}] |
| 2 | createBackup("FULL", []) | [{fileName: "a.txt", content: "A1"}] |
| 3 | createBackup("DIFFERENTIAL", [{fileName: "a.txt", content: "A2"}]) | [{fileName: "a.txt", content: "A2"}] |
| 4 | createBackup("DIFFERENTIAL", [{fileName: "a.txt", content: "A1"}]) | [] |
| 5 | createBackup("LOG", [{fileName: "x", content: "1"}, {fileName: "x", content: "2"}]) | [{fileName: "x", content: "1"}, {fileName: "x", content: "2"}] |
Constraints
backupTypeisFULL,DIFFERENTIAL, orLOG.- Each writes list contains at most 10,000 objects.
- File names contain from 1 through 100 Unicode scalar values.
- File content contains at most 1,000 Unicode scalar values and may be empty.
- A Unicode scalar value is a Unicode code point other than a surrogate code point.
- At most 10,000 distinct file names, 100,000 total writes, and 10,000 calls occur in one testcase.
- Files cannot be deleted.
Notes
Calls are sequential on one system and take effect in invocation order. createBackup does not wait for an external event.