FastPrepCloud Storage Compression and Decompression

Cloud Storage Compression and Decompression

Ramp logoRamp● HardNEW GRADFULLTIMEINTERNOA
Learn

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.

Finding Files

  • ["FIND_FILES", prefix, suffix]: find every current file name that starts with prefix and ends with suffix. Order matches by size descending and then name ascending, format each as name(size), and join them with ", ". Return the empty string when no file matches.

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.

Example 2

operations = [["ADD_FILE","/logs/z.txt","4"],["ADD_FILE","/logs/a.txt","8"],["ADD_FILE","/logs/b.log","8"],["FIND_FILES","/logs/",".txt"],["FIND_FILES","/tmp/",".txt"]]return = ["true","true","true","/logs/a.txt(8), /logs/z.txt(4)",""]

The two .txt files are ordered by size descending. The unrelated .log file is excluded, and the unmatched prefix returns the empty string.

Constraints

  • 1 <= operations.length <= 2000.
  • Every operation uses one documented opcode and exact arity.
  • File names, prefixes, suffixes, and user IDs contain at most 100 printable ASCII characters; required names and user IDs are non-empty.
  • Every file size is a positive even integer at most 10^9.
  • Every user capacity is an integer in [0, 10^18].
  • All capacity and used-space arithmetic fits in a signed 64-bit integer.

Source note: Four original CodeSignal screenshots show the cumulative Ramp cloud-storage contract across file operations, filtered search, user capacities, and compression/decompression.

More Ramp problems

See Ramp hiring insights
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…