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:

  • name
  • size (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 None if 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 None if 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

  1. alice owns a.txt (10) and b.txt (20). backup_user("alice") returns 2.
  2. After deleting a.txt and adding c.txt (30), restoring alice brings back a.txt (10) and b.txt (20), removes c.txt, and returns 2.
  3. If another user owns a.txt before restoration, a.txt is 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: