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.

Bucket Structure

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: count /A/{1,2}.


Click here to see solution. We compose the following command that returns a different file count for each possible case.
count /B/1 /C/1 /C/2 /D/1 /D/2 /D/3
The cases and their corresponding file count are the following.
 Directory that has more files  |  File count
            A                   |     6000
            B                   |     6001
            C                   |     6002
            D                   |     6003
By inspecting the output of a single run of the command, we determine the directory that has more files.

Want to read more?

I regularly write solutions to programming problems that you may or may not find in technical job interviews. I also love to explain those solutions and answer questions. If you would like to get the latest problem + solution, subscribe to the newsletter or subscribe via RSS. You can also follow me on Twitter, GitHub, and LinkedIn.