File Paths and Trees
A coding challenge does not have to be convoluted to test your coding skills. This is a two-part coding challenge seen in the wild at a FAANG interview.
Part 1: Paths to trees
Given a list of file paths, convert them to a tree. A path is either
/ or a sequence of one or more alphanumeric components, each
preceded by a single /. You are free to choose the data structure
for your tree. You can use any language you like.
Example input.
/
/tmp/tmux1000/default
/tmp/snap
/tmp/snap/firefox
/tmp/snap/thunderbird
Example tree.
/ --> tmp --> tmux1000 -> default
\
-> snap --> firefox
\
-> thunderbird
Implementation of part 1 in Ruby
The execution time of function paths_to_tree(paths) is bounded by O(nm)
where n is the number of paths and m is the length of the longest
path.
#!/usr/bin/env ruby
def paths_to_tree(paths)
tree = {}
paths.each do |path|
comps = path.split('/')
curr = tree
comps[1..-1]&.each do |comp|
curr[comp] ||= {}
curr = curr[comp]
end
end
return tree
end
def pretty_tree(tree, margin=0, parent='/')
puts("#{' ' * margin}#{parent}")
tree.keys.each do |key|
pretty_tree(tree[key], margin + 2, key)
end
end
[
[
'/',
'/tmp/tmux1000/default',
'/tmp/snap',
'/tmp/snap/firefox',
'/tmp/snap/thunderbird'
]
].each do |paths|
tree = paths_to_tree(paths)
pretty_tree(tree)
endPart 2: Tree to paths
Extend your code so that given the tree generated from a list of paths you can reconstruct the paths in the list of paths. Return the paths in any order. Return each distinct path once.
Example input. The nodes of the tree that correspond to input
paths from part 1 are indicated by the text (path).
/ (path) --> tmp --> tmux1000 -> default (path)
\
-> snap (path) --> firefox (path)
\
-> thunderbird (path)
Example output. The same input as in part 1.
/
/tmp/tmux1000/default
/tmp/snap
/tmp/snap/firefox
/tmp/snap/thunderbird
Implementation of part 2 in Ruby
We extend the solution function from part 1 paths_to_tree(paths) so
that it tags tree nodes that correspond to input paths. We implement
the reconstruction of paths from a given tree in function
tree_to_paths(tree). We highlight the changes to the solution
function from part 1 paths_to_tree(paths) by adding a comment #
*** on each line that changed.
The execution time of tree_to_paths(tree) is bounded by O(nm),
where n is the number of path nodes in the tree and m is the
length of the longest path. The joining of the path components with
/ is what dominates the execution time because
do_tree_to_paths(...) visits each tree node once and for each path
node concatenates the path components that correspond to the node.
#!/usr/bin/env ruby
def paths_to_tree(paths)
tree = {}
paths.each do |path|
comps = path.split('/')
curr = tree
comps[1..-1]&.each do |comp|
curr[comp] ||= {}
curr = curr[comp]
end
curr[:path] = true # ***
end
return tree
end
def pretty_tree(tree, margin=0, parent='/')
is_path = tree[:path] ? ' (path)' : '' # ***
puts("#{' ' * margin}#{parent}#{is_path}") # ***
tree.keys.each do |key|
next if key == :path # ***
pretty_tree(tree[key], margin + 2, key)
end
end
def do_tree_to_paths(tree, path, paths)
if tree[:path]
if path.empty?
paths << '/'
else
paths << "/#{path.join('/')}"
end
end
tree.keys.each do |key|
next if key == :path
path.push(key)
do_tree_to_paths(tree[key], path, paths)
path.pop()
end
end
def tree_to_paths(tree)
paths = []
do_tree_to_paths(tree, [], paths)
return paths
end
[
[
'/',
'/tmp/tmux1000/default',
'/tmp/snap',
'/tmp/snap/firefox',
'/tmp/snap/thunderbird'
]
].each do |paths|
puts('****************************************')
puts('PART 1')
tree = paths_to_tree(paths)
pretty_tree(tree)
puts('****************************************')
puts('PART 2')
reconstructed_paths = tree_to_paths(tree)
reconstructed_paths.each { |p| puts(p) }
end