FastPrepUnix-Like File Search with Composable Filters

Unix-Like File Search with Composable Filters

Amazon logoAmazon● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

There are n nodes identified by index. paths[i] is node i's unique canonical absolute path. kinds[i] is 0 for a directory, 1 for a regular file, or 2 for a symbolic link. sizes[i] is a regular file's size in bytes. For a directory, children[i] lists its direct child node IDs. For a symbolic link, linkTargets[i] is its target node; all other entries are -1.

Starting from node root, recursively follow directory children and symbolic links. Visit each underlying node at most once, so cycles terminate and a file reachable through several paths is returned once. Only regular files are eligible results.

A regular file matches when its basename contains nameContains as a case-sensitive substring and its size is at least minSize. Return the matching canonical absolute paths in lexicographic order.

Function

searchFiles(paths: String[], kinds: int[], sizes: int[], children: int[][], linkTargets: int[], root: int, nameContains: String, minSize: int) → String[]

Examples

Example 1

paths = ["/","/logs","/logs/app.log","/logs/app.tmp","/latest"]kinds = [0,0,1,1,2]sizes = [0,0,900,120,0]children = [[1,4],[2,3],[],[],[]]linkTargets = [-1,-1,-1,-1,1]root = 0nameContains = ".log"minSize = 500return = ["/logs/app.log"]

The regular log file satisfies both filters. The symbolic link reaches the same directory, but node identity prevents duplicate traversal.

Example 2

paths = ["/","/data","/data/report.csv","/data/report.tmp","/alias","/data/back"]kinds = [0,0,1,1,2,2]sizes = [0,0,400,150,0,0]children = [[1,4],[2,3,5],[],[],[],[]]linkTargets = [-1,-1,-1,-1,1,0]root = 0nameContains = "report"minSize = 200return = ["/data/report.csv"]

The CSV file satisfies both filters; the temporary file is too small. The back-link cycle terminates after the root is recognized as visited.

Constraints

  • 1 <= n <= 100000.
  • All node arrays have length n, and every referenced node ID is valid.
  • Every path is a unique absolute path. A regular file's basename is the suffix after its final /.
  • Directory child lists contain no duplicate IDs. Regular files and symbolic links have empty child lists.
  • 0 <= sizes[i], minSize <= 1000000000.
  • nameContains has length from 0 through 100.
  • Every symbolic link has one valid target; other linkTargets entries equal -1.

More Amazon problems

See Amazon hiring insights
public String[] searchFiles(String[] paths, int[] kinds, int[] sizes, int[][] children, int[] linkTargets, int root, String nameContains, int minSize) {
    // TODO: traverse each reachable node once and apply both file filters.
}
paths["/","/logs","/logs/app.log","/logs/app.tmp","/latest"]
kinds[0,0,1,1,2]
sizes[0,0,900,120,0]
children[[1,4],[2,3],[],[],[]]
linkTargets[-1,-1,-1,-1,1]
root0
nameContains".log"
minSize500
expected["/logs/app.log"]
Checking account…