FastPrepCloud Storage System

Cloud Storage System

Anthropic logoAnthropic● MediumFULLTIMEOA
Learn

Problem statement

Implement an in-memory cloud storage system. The original assessment exposes a stateful class whose features are unlocked cumulatively across four levels. Do not access the real file system.

FastPrep operation-sequence adapter

FastPrep calls cloudStorageSystem(operations) once. Process the rows of operations from left to right while preserving one shared storage state. Each row begins with an uppercase operation name followed by that method's string arguments.

Return one string-array row for every input operation:

  • Encode a Boolean as ["true"] or ["false"].
  • Encode an integer as a one-element row such as ["10"].
  • Encode None as an empty row [].
  • For GET_N_LARGEST, return its list of formatted file strings directly; an empty list is also [].

Level 1: file operations

  • ["ADD_FILE", name, size] implements add_file(name, size). Add a new file with the given byte size. Return true, or false if the name already exists. Files added this way belong to the unlimited admin user.
  • ["GET_FILE_SIZE", name] implements get_file_size(name). Return the file size, or None if the file does not exist.
  • ["DELETE_FILE", name] implements delete_file(name). Delete the file and return its former size, or return None if it does not exist.

Level 2: largest files

  • ["GET_N_LARGEST", prefix, n] implements get_n_largest(prefix, n). Select files whose names start with prefix, order them by size descending and then by name lexicographically ascending, and return at most n entries formatted as name(size). Return every match when fewer than n exist and an empty list when none exist.

Level 3: users and capacity

All users share the same global file-name space.

  • ["ADD_USER", userId, capacity] implements add_user(userId, capacity). Create a user with that byte limit. Return false when the user already exists and true otherwise.
  • ["ADD_FILE_BY", userId, name, size] implements add_file_by(userId, name, size). Add the file for that user only when the user exists, the name is free, and the user's total owned size would not exceed the capacity. Return the remaining capacity after success, or None on failure.
  • ["MERGE_USER", userId1, userId2] implements merge_user(userId1, userId2). If both users exist and are distinct, transfer every file owned by userId2 to userId1, combine their capacity limits, delete userId2, and return userId1's remaining capacity. Return None when either user is missing or the IDs are equal. Neither merge argument is admin.

Level 4: backup and restore

  • ["BACKUP_USER", userId] implements backup_user(userId). Replace that user's previous backup with an independent snapshot of the names and sizes of all files currently owned by the user. Return the number of backed-up files, or None when the user does not exist.
  • ["RESTORE_USER", userId] implements restore_user(userId). Return None when the user does not exist. Otherwise delete all files currently owned by the user. If a backup exists, restore each backed-up file whose name is not currently occupied by another user; skip occupied names. With no backup, leave the user with no files. Return the number of restored files.

Normal file additions, deletions, and other users' operations never mutate a stored backup. Merging leaves userId1's backup unchanged and deletes userId2's backup. Restoring files does not change the user's capacity limit.

Function

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

Examples

Example 1

operations = [["ADD_FILE","/dir/file1.txt","5"],["ADD_FILE","/dir/file2","20"],["ADD_FILE","/dir/deeper/file3.mov","9"],["GET_N_LARGEST","/dir","2"],["ADD_FILE","/big_file.mp4","20"],["GET_N_LARGEST","/","2"],["DELETE_FILE","/dir/file1.txt"],["GET_FILE_SIZE","/dir/file1.txt"]]return = [["true"],["true"],["true"],["/dir/file2(20)","/dir/deeper/file3.mov(9)"],["true"],["/big_file.mp4(20)","/dir/file2(20)"],["5"],[]]

The first three operations add files. The first ranking returns the two largest names under /dir. After adding /big_file.mp4, the two size-20 files tie, so their names decide the order. Deleting /dir/file1.txt returns its former size, and the following lookup returns an empty row for None.

Example 2

operations = [["ADD_USER","user1","100"],["ADD_USER","user2","40"],["ADD_FILE_BY","user1","/a","60"],["ADD_FILE_BY","user2","/b","10"],["ADD_FILE_BY","user2","/c","30"],["MERGE_USER","user1","user2"],["GET_FILE_SIZE","/b"],["ADD_FILE_BY","user2","/d","1"],["ADD_FILE_BY","user1","/d","40"],["MERGE_USER","user1","user1"]]return = [["true"],["true"],["40"],["30"],["0"],["40"],["10"],[],["0"],[]]

Before the merge, user1 has 40 bytes remaining and user2 has none. The merge transfers /b and /c, deletes user2, and leaves user1 with 40 bytes. Calls for the deleted user and a self-merge return empty rows.

Example 3

operations = [["ADD_USER","user","100"],["ADD_FILE_BY","user","/file3.mp4","60"],["ADD_FILE_BY","user","/file4.txt","10"],["BACKUP_USER","user"],["DELETE_FILE","/file3.mp4"],["DELETE_FILE","/file4.txt"],["ADD_FILE","/file3.mp4","140"],["ADD_FILE_BY","user","/dir/file5.new","20"],["RESTORE_USER","user"],["GET_FILE_SIZE","/file3.mp4"],["GET_FILE_SIZE","/file4.txt"],["GET_FILE_SIZE","/dir/file5.new"]]return = [["true"],["40"],["30"],["2"],["60"],["10"],["true"],["80"],["1"],["140"],["10"],[]]

The backup remembers /file3.mp4 and /file4.txt. Before restore, the admin occupies the former name and the user owns /dir/file5.new. Restore deletes the user's current file, skips the conflicting name, restores /file4.txt, and returns 1.

Constraints

  • Every operation row is well-formed and uses one of the operation names described above.
  • The integer arguments from the source are supplied as base-10 strings that fit the signed 64-bit runtime representation.
  • The input does not create collisions between file and directory names.
  • All users share one file-name namespace; admin exists initially and has unlimited capacity.
  • Process operations in the supplied order. The system is empty initially except for admin.

More Anthropic problems

See Anthropic hiring insights
public String[][] cloudStorageSystem(String[][] operations) {
  // Process the operations in order and return one result row per operation.
  // write your code here
}
operations[["ADD_FILE","/dir/file1.txt","5"],["ADD_FILE","/dir/file2","20"],["ADD_FILE","/dir/deeper/file3.mov","9"],["GET_N_LARGEST","/dir","2"],["ADD_FILE","/big_file.mp4","20"],["GET_N_LARGEST","/","2"],["DELETE_FILE","/dir/file1.txt"],["GET_FILE_SIZE","/dir/file1.txt"]]
expected[["true"], ["true"], ["true"], ["/dir/file2(20)", "/dir/deeper/file3.mov(9)"], ["true"], ["/big_file.mp4(20)", "/dir/file2(20)"], ["5"], []]
Checking account…