TradeDeskOA #1
Cloud Storage System (4 Progressive Levels)
估计中等评测数据准备中,可先阅读题面
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 filenameto the storage.capacityis its size in bytes. The current operation fails if a file with the same name already exists. Returnstrueif added successfully orfalseotherwise.Optional<Integer> getFileSize(String name)— return the size of the file if it exists, orOptional.empty()otherwise.Optional<Integer> deleteFile(String name)— delete the file. Returns the deleted file's size if successful, orOptional.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 topnlargest files with names starting withprefix, in the format"name1(size1)", "name2(size2)", ...". Sorted by size descending; tie broken by name ascending. Empty list if no match; fewer thannmatches just returns all of them.
Level 3 — Multi-User with Capacity Limits
boolean addUser(String userId, int capacity)— add a new user. Fails ifuserIdexists.Optional<Integer> addFileBy(String userId, String name, int size)— likeaddFilebut 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 byuserId(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 ofuserId's files are deleted. Files whose name was taken by another user after backup are skipped. Returns the number of successfully restored files.mergeUserdoes not affectuserId1's backup;userId2's backup is deleted along with the user.