cswork / OA打开练习工作台

TradeDeskOA #1

Cloud Storage System (4 Progressive Levels)

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)迭代器设计哈希计数字符串

同类可判练习:文件存储(四关递进)

Implement a simple cloud storage system. All operations should be supported as listed below. The task consists of several levels; subsequent levels are opened when the current level is correctly solved. The system should be in-memory; you don't need to work with the real filesystem.

It is guaranteed that the given queries will never call operations that result in collisions between file and directory names.

Level 1 — Basic File Manipulation

  • boolean addFile(String name, int capacity) — add a new file name to the storage. capacity is its size in bytes. The current operation fails if a file with the same name already exists. Returns true if added successfully or false otherwise.
  • Optional<Integer> getFileSize(String name) — return the size of the file if it exists, or Optional.empty() otherwise.
  • Optional<Integer> deleteFile(String name) — delete the file. Returns the deleted file's size if successful, or Optional.empty() if the file does not exist.

Level 2 — Prefix Queries

  • List<String> getNFilesByPrefix(String prefix, int n) — return the list of strings representing names of the top n largest files with names starting with prefix, in the format "name1(size1)", "name2(size2)", ...". Sorted by size descending; tie broken by name ascending. Empty list if no match; fewer than n matches just returns all of them.

Level 3 — Multi-User with Capacity Limits

  • boolean addUser(String userId, int capacity) — add a new user. Fails if userId exists.
  • Optional<Integer> addFileBy(String userId, String name, int size) — like addFile but checks user's remaining quota. Returns new remaining capacity on success.
  • Optional<Integer> mergeUser(String userId1, String userId2) — merge user2 into user1: user2's files become owned by user1, user1's capacity gains user2's remaining capacity. Returns user1's new total capacity. Optional.empty() if either missing or ids equal.

All Level 1 addFile calls are made by user "admin" who has unlimited storage.

Level 4 — Backup and Restore

  • Optional<Integer> backupUser(String userId) — snapshot all files owned by userId (names + sizes) on a separate store, isolated from later mutations. Overwrites previous backup. Returns the number of backed-up files.
  • Optional<Integer> restoreUser(String userId) — restore the latest backup. If no backup exists, all of userId's files are deleted. Files whose name was taken by another user after backup are skipped. Returns the number of successfully restored files. mergeUser does not affect userId1's backup; userId2's backup is deleted along with the user.