You are given a bucket with four directories in a very limited imitation of Amazon S3. Your task is to find the directory that has more files than any of the other three by issuing a given CLI command only once.
The structure of the directories is illustrated by the following diagram.
Three of the four directories contain four subdirectories and each of those subdirectories contains 1000 files. The remaining directory contains four subdirectories and each of those subdirectories contains 1001 files.
You may only use CLI command
count. The Syntax of the command is
count <file list>. An element of the file list may be a file, a
directory or a pattern. A pattern may consist of any glob pattern,
regular expression, and brace expansion that Bash accepts (reference
here). The command
returns the file count that corresponds to the file list. For
example, when the subdirectories of directory
A contain 1000 files
each, the output of command
count /A/1 /A/2 is 2000. The following
command uses brace expansion and gives the same output:
Click here to see solution.We compose the following command that returns a different file count for each possible case. The cases and their corresponding file count are the following. By inspecting the output of a single run of the command, we determine the directory that has more files.
Want to read more?
I love to explain and answer questions on programming problems, the kind you find in coding interviews. I publish a new programming problem and its solution every Sunday. Did I mention that I love to answer questions?