Problem · Design

In-Memory Vault File System

Learn this problem
MediumHarvey logoHarveyFULLTIMEONSITE INTERVIEW

Problem statement

Implement an in-memory vault that stores files in directories. You are given equal-length arrays operations and paths.

Process each operation in order:

  • add_file stores the file at the absolute path paths[i]. Missing parent directories are created automatically. If that exact filename already exists in its directory, insert (x) immediately before the final extension, where x is the smallest positive integer that produces an unused filename. The final extension begins at the last period when that period is neither the first nor the last character. A filename without such an extension receives (x) at the end.
  • get_files treats paths[i] as an absolute directory path and returns the names of its immediate files in lexicographic order. A missing or empty directory returns an empty list.

Return one row for every get_files operation, in the order those queries occur. File names in a row do not include the directory prefix.

Function

runVault(operations: String[], paths: String[]) → String[][]

Examples

Example 1

operations = ["add_file","add_file","add_file","get_files"]paths = ["/docs/report.txt","/docs/report.txt","/docs/image.png","/docs"]return = [["image.png","report(1).txt","report.txt"]]

The second report collides with report.txt, so it becomes report(1).txt. The query lists only immediate files in sorted order.

Example 2

operations = ["add_file","add_file","get_files","get_files"]paths = ["/a/b/note","/a/c/note","/a/b","/a"]return = [["note"],[]]

The first query sees the immediate file in /a/b. The vault does not return descendant files recursively, so querying /a returns an empty list.

Constraints

  • 0 <= operations.length <= 200000.
  • operations.length == paths.length.
  • Every operation is add_file or get_files.
  • Every path is absolute, uses a single / separator between nonempty components, and contains neither . nor .. directory components.
  • An add_file path contains at least one directory and one nonempty filename.
  • Path components contain lowercase English letters, digits, hyphens, underscores, and periods.
  • The total path length across all operations is at most 1000000.

More Harvey problems

drafts saved locally
public String[][] runVault(String[] operations, String[] paths) {
  // write your code here
}
operations["add_file","add_file","add_file","get_files"]
paths["/docs/report.txt","/docs/report.txt","/docs/image.png","/docs"]
expected[["image.png", "report(1).txt", "report.txt"]]
checking account