Unix Find with Filters and Links

Asked at Amazon· Screen

Reported by the problem's creator, not verified

Import another question

You work on an in-memory file tree that models a Unix-like file system. A node is one of three kinds:

  • a directory {"name": ..., "children": [...]},
  • a file {"name": ..., "size": ...},
  • a symbolic link {"name": ..., "target": ...}, where target is an absolute path.

The root is a directory named "". Names are non-empty, contain no /, and are unique among siblings. The canonical path of a node is / followed by the names from the root down to it, joined with /; the root's canonical path is /.

A link's target is looked up as a canonical path, without resolving through other links along the way. A target that names no node in the tree (for example one that goes through a link, such as /x/ln/f where /x/ln is a link), or that names another link, is broken.

Examples

  1. Example 1

    Input: {"mode":"and","root":{"name":"","children":[{"name":"docs","children":[{"name":"report.txt","size":900},{"name":"report.pdf","size":5000},{"name":"notes.txt","size":20}]},{"name":"logs","children":[{"name":"app.log","size":7000},{"name":"old","children":[{"name":"app.log.1","size":70000},{"name":"app.txt","size":10}]}]},{"name":"report.bak","size":6000}]},"start":"/","min_size":1000,"name_query":"report","follow_links":false}

    Output: ["/docs/report.pdf","/report.bak"]

    Explanation: report.pdf (5000) and report.bak (6000) contain report and are strictly above 1000. report.txt (900) fails the size test and notes.txt fails the name test. The two matching paths come back sorted.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.