Problem · Hash Table

Cloud Storage File Versioning

Learn this problem
MediumAnthropicOA

Problem statement

Implement an in-memory cloud storage system that keeps every version of an overwritten file. Process the operations in order and return one string result per operation.

File Operations

  • ["ADD_FILE", file_name, size]: create a file and return created. If the name already exists, append a new version with the supplied size and return overwritten. Versions are numbered from 1.
  • ["GET_FILE_SIZE", file_name]: return the latest version's size, or an empty string if the file does not exist.
  • ["MOVE_FILE", name_from, name_to]: move the file and all of its versions. Return false if the source is missing or the destination already exists; otherwise return true.
  • ["GET_LARGEST_N", prefix, n]: return up to n prefix-matching files, using each file's latest size. Sort by size descending and file name ascending; format as name(size), name(size). Return an empty string when no file matches.

Version Operations

  • ["GET_VERSION", file_name, version]: return that version's size, or an empty string if the file or version does not exist.
  • ["DELETE_VERSION", file_name, version]: permanently delete that version and return true. Return false when it does not exist.
  • After deletion, every higher version number decreases by one. Deleting a file's only version permanently removes the file.

Function

cloudStorageFileVersioning(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD_FILE","/file-a.txt","6"],["ADD_FILE","/file-a.txt","3"],["GET_VERSION","/file-a.txt","2"],["GET_VERSION","/file-a.txt","4"],["GET_VERSION","/file-a.txt","1"],["DELETE_VERSION","/file-a.txt","1"],["GET_VERSION","/file-a.txt","1"]]return = ["created","overwritten","3","","6","true","3"]

The second write creates version 2. Deleting version 1 renumbers the former version 2 to version 1.

Constraints

  • All integer parameters are between 1 and 200.

More Anthropic problems

drafts saved locally
public String[] cloudStorageFileVersioning(String[][] operations) {
  // write your code here
}
operations[["ADD_FILE","/file-a.txt","6"],["ADD_FILE","/file-a.txt","3"],["GET_VERSION","/file-a.txt","2"],["GET_VERSION","/file-a.txt","4"],["GET_VERSION","/file-a.txt","1"],["DELETE_VERSION","/file-a.txt","1"],["GET_VERSION","/file-a.txt","1"]]
expected["created", "overwritten", "3", "", "6", "true", "3"]
checking account