Print All File and Folder Paths
Updated
Problem statement
A file system is stored as a tree of n entries labeled 0 to n - 1. Entry 0 is the root folder: names[0] is "/" and parent[0] is -1. Every other entry i is named names[i] and sits inside the folder with label parent[i]. An entry can be a file or a folder, and you do not need to tell them apart.
The full path of the root is "/". The full path of any other entry is the names on the way from the root down to that entry, each written after a /. For example, an entry named locker inside a folder named combinations, which sits in the root, has the full path "/combinations/locker".
Return the full paths of all n entries in this order:
- Start at the root.
- List an entry's path, then the paths of everything inside it, before moving on to the next entry in the same folder.
- Visit the entries of one folder in increasing order of their names. Names are compared character by character by character code, and when one name is the start of another, the shorter name comes first. For example,
"docs"comes before"docs-old","a-b"comes before"a.b", and"10"comes before"9".
Function
listAllPaths(names: String[], parent: int[]) → String[]Examples
Example 1
names = ["/","foo","combinations","b","luggage","a","locker"]parent = [-1,0,0,1,2,1,2]return = ["/","/combinations","/combinations/locker","/combinations/luggage","/foo","/foo/a","/foo/b"]The root folder holds foo and combinations. Folder foo holds b and a, and folder combinations holds luggage and locker.
- Inside the root,
combinationscomes beforefoo, so everything inside/combinationsis listed before/foo. - Inside
combinations,lockercomes beforeluggagebecause their second letters areoandu, andocomes first. - Inside
foo,acomes beforeb, even thoughbhas the smaller label.
Example 2
names = ["/","docs-old","a.md","docs"]parent = [-1,0,3,0]return = ["/","/docs","/docs/a.md","/docs-old"]The root holds docs-old (entry 1) and docs (entry 3). Entry 2, a.md, is inside docs; its parent has a larger label than it does.
docs comes before docs-old because it is the start of that name. So /docs and everything inside it come first, then /docs-old.
Sorting the full paths as plain strings would give a different order: /docs-old would come before /docs/a.md, because - has a smaller character code than /.
Example 3
names = ["/"]parent = [-1]return = ["/"]Only the root folder exists, so the only path is "/".
Constraints
1 <= names.length <= 200parent.length == names.lengthnames[0]is"/"andparent[0] == -1.- For every
i >= 1,names[i]has 1 to 20 characters, and each character is a lowercase English letter, a digit,.,-or_. - For every
i >= 1,0 <= parent[i] <= names.length - 1, and the parent links form one tree rooted at entry0. - Entries in the same folder have different names.
- Every full path has at most
100characters.
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
- Odd Numbers Up and BackPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Root-to-Leaf Paths as StringsAdobe · PHONE SCREENAdobe interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Canonical N-Ary Tree CodecApple · PHONE SCREENApple interview questions
- Aggregate Machine TopologyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Find File Paths in a Directory TreeUber · PHONE SCREENUber interview questions