Google Onsite Interview
Anonymous User
12209

I recently had a google interview, and I was asked the below question, let me know if you get it.

Basically I needed to implement cli.

I was given (as strings) as directories e.g.
/a/b/x.txt
/a/b/p.txt
/a/c
/a/d/y.txt
/a/d/z.txt

Also, I was given the selected directories e.g.
/a/d/y.txt
/a/d/z.txt
/a/b/p.txt

My output should be
/a/d
/a/b/p.txt

/a/d
is the answer because it has 2 txt files (y and z), and both are selected.
/a/b/p.txt
is the answer because another file in the directory i.e. /a/b/x.txt is not selected, if it was selected, answer would have been /a/b

Basically, if all items are selected in a particular directory, we need to return the just prev directory.

I tried solving it, assuming the directories to be a tree, and used dfs. I messed up really bad.
How can we solve this problem? If possible, can someone code it up?

Comments (31)