Problem · Hash Table

Versioned In-Memory File System

Learn this problem
MediumHashiCorp logoHashiCorpFULLTIMEOA

Problem statement

Simulate an in-memory file system. Each current file has a path, positive size, and owner. Process the operation rows in order and return one string per row.

OperationResult
["CREATE", time, path, size, owner]Create an absent path and return true; otherwise return false.
["DELETE", time, path]Delete an existing path and return true; otherwise return false.
["SET_OWNER", time, path, owner]Change an existing file's owner and return true; otherwise return false.
["BACKUP", time]Save an immutable snapshot under that time and return the number of files saved.
["RECOVER", time, backupTime]Replace the current files with the exact named snapshot and return true. Return false when that backup does not exist.

Operation times are strictly increasing. Recovery does not remove saved backups, and a failed mutation leaves all state unchanged.

Function

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

Examples

Example 1

operations = [["CREATE","1","/a","5","alice"],["BACKUP","2"],["SET_OWNER","3","/a","bob"],["CREATE","4","/b","2","bob"],["RECOVER","5","2"],["DELETE","6","/a"],["DELETE","7","/b"]]return = ["true","1","true","true","true","true","false"]

Recovery restores the one-file snapshot from time 2, so /b no longer exists after /a is deleted.

Example 2

operations = [["CREATE","10","/x","3","a"],["CREATE","11","/x","4","b"],["BACKUP","12"],["DELETE","13","/x"],["RECOVER","14","99"],["RECOVER","15","12"]]return = ["true","false","1","true","false","true"]

The duplicate create fails. Recovery from missing backup 99 fails without changing state, while backup 12 restores /x.

Constraints

  • 1 <= operations.length <= 100000.
  • Every row has one of the documented shapes, and operation times are strictly increasing decimal integers.
  • Paths and owner names contain between 1 and 100 printable ASCII characters.
  • 1 <= size <= 10^9.
  • Backup times are unique because operation times are unique.

More HashiCorp problems

drafts saved locally
public String[] runVersionedFileSystem(String[][] operations) {
    // Write your code here.
}
operations[["CREATE","1","/a","5","alice"],["BACKUP","2"],["SET_OWNER","3","/a","bob"],["CREATE","4","/b","2","bob"],["RECOVER","5","2"],["DELETE","6","/a"],["DELETE","7","/b"]]
expected["true", "1", "true", "true", "true", "true", "false"]
checking account