Problem · Hash Table

Cloud Storage Compression and Decompression

Learn this problem
HardAnthropicOA

Problem statement

Implement the compression level of an in-memory cloud storage system with user ownership and capacity limits. Return one string result per operation. Use the literal string null for an empty optional result.

Base File Operations

  • ["ADD_FILE", name, size]: add an admin-owned file. The admin has unlimited capacity. Return true, or false when the name already exists.
  • ["COPY_FILE", name_from, name_to]: copy a file when the source exists and the destination does not. Preserve the source owner's identity and return true; otherwise return false.
  • ["GET_FILE_SIZE", name]: return the file size, or null when missing.

Users and Capacity

  • ["ADD_USER", user_id, capacity]: create a unique user and return true, or false when the ID exists.
  • ["ADD_FILE_BY", user_id, name, size]: add an owned file without exceeding capacity. Return the user's remaining capacity, or null on failure.
  • ["UPDATE_CAPACITY", user_id, capacity]: replace the user's capacity. If current files exceed it, remove the largest files until usage fits; break equal-size ties by file name in lexicographic order. Return the number removed, or null for a missing user.

Compression

  • ["COMPRESS_FILE", user_id, name]: require an existing file owned by the user. Replace it with name + ".COMPRESSED", preserve ownership, and halve its size. File sizes supplied to this operation are guaranteed even. Return remaining capacity, or null on failure.
  • The input name to COMPRESS_FILE never already ends in .COMPRESSED. ADD_FILE and ADD_FILE_BY are never used to introduce compressed names. COPY_FILE preserves the suffix.
  • ["DECOMPRESS_FILE", user_id, name]: name ends in .COMPRESSED. Require that it exists and belongs to the user. Replace it with the suffix-free name and double its size.
  • Decompression fails with null if the uncompressed name already exists or the larger file would exceed the user's capacity. On success, return remaining capacity.

Runner Interface

cloudStorageCompression accepts String[][] operations and returns a String[].

Function

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

Examples

Example 1

operations = [["ADD_USER","user1","1000"],["ADD_USER","user2","5000"],["ADD_FILE_BY","user1","/dir/file.mp4","500"],["COMPRESS_FILE","user2","/dir/file.mp4"],["COMPRESS_FILE","user3","/dir/file.mp4"],["COMPRESS_FILE","user1","/folder/non_existing_file"],["COMPRESS_FILE","user1","/dir/file.mp4"],["GET_FILE_SIZE","/dir/file.mp4.COMPRESSED"],["GET_FILE_SIZE","/dir/file.mp4"],["COPY_FILE","/dir/file.mp4.COMPRESSED","/file.mp4.COMPRESSED"],["ADD_FILE_BY","user1","/dir/file.mp4","500"],["DECOMPRESS_FILE","user1","/dir/file.mp4.COMPRESSED"],["UPDATE_CAPACITY","user1","2000"],["DECOMPRESS_FILE","user2","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user3","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user1","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user1","/file.mp4.COMPRESSED"]]return = ["true","true","500","null","null","null","750","250","null","true","0","null","0","null","null","null","750"]

Only User 1 owns the original file. Compression reduces it from 500 to 250. The first decompression attempt lacks capacity, and a later attempt conflicts with the re-created uncompressed name. After the capacity increase, the copied compressed file can be decompressed successfully.

More Anthropic problems

drafts saved locally
public String[] cloudStorageCompression(String[][] operations) {
  // write your code here
}
operations[["ADD_USER","user1","1000"],["ADD_USER","user2","5000"],["ADD_FILE_BY","user1","/dir/file.mp4","500"],["COMPRESS_FILE","user2","/dir/file.mp4"],["COMPRESS_FILE","user3","/dir/file.mp4"],["COMPRESS_FILE","user1","/folder/non_existing_file"],["COMPRESS_FILE","user1","/dir/file.mp4"],["GET_FILE_SIZE","/dir/file.mp4.COMPRESSED"],["GET_FILE_SIZE","/dir/file.mp4"],["COPY_FILE","/dir/file.mp4.COMPRESSED","/file.mp4.COMPRESSED"],["ADD_FILE_BY","user1","/dir/file.mp4","500"],["DECOMPRESS_FILE","user1","/dir/file.mp4.COMPRESSED"],["UPDATE_CAPACITY","user1","2000"],["DECOMPRESS_FILE","user2","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user3","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user1","/dir/file.mp4.COMPRESSED"],["DECOMPRESS_FILE","user1","/file.mp4.COMPRESSED"]]
expected["true", "true", "500", "null", "null", "null", "750", "250", "null", "true", "0", "null", "0", "null", "null", "null", "750"]
checking account