FastPrepVersioned In-Memory File System

Versioned In-Memory File System

HashiCorp logoHashiCorp● MediumFULLTIMEOA

Updated

Problem statement

Simulate an in-memory file system. Each file has a path, a positive size and an owner. Go through the rows of operations in order and return one string per row.

OperationWhat it does and returns
["CREATE", time, path, size, owner]If no file has this path, create it and return true. Otherwise return false.
["DELETE", time, path]If the file exists, delete it and return true. Otherwise return false.
["SET_OWNER", time, path, owner]If the file exists, change its owner and return true. Otherwise return false.
["BACKUP", time]Save a copy of all current files under this time. Later changes never alter the copy. Return the number of files saved.
["RECOVER", time, backupTime]If a backup was saved under backupTime, replace all current files with that backup and return true. Otherwise return false.

Times only go up from row to row. Recovering keeps the backups, so the same backup can be used again. An operation that returns false changes nothing. Every part of every row is a string, numbers included.

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"]

/a is created (true), and the backup at time 2 saves 1 file. Then /a gets owner bob (true) and /b is created (true). Recovering backup 2 brings back the files as they were at time 2: only /a, owned by alice (true). So deleting /a returns true, and deleting /b returns false because /b is not in that backup.

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"]

/x is created (true). Creating /x again fails because it exists (false). The backup at time 12 saves 1 file. Deleting /x returns true. No backup was saved at time 99, so that recover fails and changes nothing (false). Recovering backup 12 brings /x back (true).

Constraints

  • 1 ≤ operations.length ≤ 100000
  • Each row has one of the shapes above, and the times are whole numbers that strictly increase from row to row.
  • Paths and owner names have 1 to 100 printable ASCII characters.
  • 1 ≤ size ≤ 10^9
  • Because times never repeat, no two backups have the same time.

More HashiCorp interview questions

Similar questions at other companies

All HashiCorp interview questionsSee HashiCorp hiring insights
Saved
class Solution {
    public String[] runVersionedFileSystem(String[][] operations) {
        // Write your code here.
    }
}
Checking account…