Cloud Storage System: User File Backup and Restore
Problem: Cloud Storage System — User Backup and Restore
Implement an in-memory simplified cloud storage system. File names are globally unique. Each file has:
namesize(a positive integer)created_by(the owner user ID)
There are regular users and an admin user. The admin can also create, back up, and restore files. Assume earlier levels already support file/user management and user merging.
Implement the following Level 4 APIs:
backup_user(user_id: str) -> int | None
restore_user(user_id: str) -> int | None
backup_user(user_id)
Create or overwrite the latest backup for user_id.
- The backup contains the current
(file name, file size)of every file owned by that user. - Backups are stored separately and are unaffected by later file mutations.
- A new backup overwrites any old backup for the same user.
- Return the number of backed-up files.
- Return
Noneif the user does not exist.
restore_user(user_id)
Restore the files owned by user_id to the state of that user's latest backup.
- Return
Noneif the user does not exist. - First remove every file currently owned by this user.
- If a backup exists, restore each file from that backup.
- If a file name is currently occupied by another user, skip that file; never overwrite another user's file.
- If no backup exists, only delete the user's current files.
- Return the number of files successfully restored.
admin must follow the same backup and restore semantics as regular users.
Example
aliceownsa.txt (10)andb.txt (20).backup_user("alice")returns2.- After deleting
a.txtand addingc.txt (30), restoringalicebrings backa.txt (10)andb.txt (20), removesc.txt, and returns2. - If another user owns
a.txtbefore restoration,a.txtis skipped while other non-conflicting backup files can still be restored.
Example
Unlock to view complete problem details
and practice with sample input/output
Was this article helpful?
View Test Cases & Run Code requires membership
Standard Input
Execution Result:
