Services

Resources

Company

Tree /exercise

Problem statement

Write a command line program that implements Unix tree like functionality. Read the man page for grep if you don't know what it does. Implement all options such as -f (print relative filepath), -d (print only directories), -L (print only specified levels of the tree), -p (print file permissions), -t (sort files by last modification time), -X (print output in XML format), -J (print output in JSON format).

The program should have the following features.

Story 1

Print tree structure recursively with proper formatting for a given directory. Also, print a summary at the end.

➜  student-grading-java git:(main) tree src
src
├── main
│   ├── java
│   │   └── in
│   │       └── one2n
│   │           └── exercise
│   │               ├── Grade.java
│   │               ├── Grader.java
│   │               └── Student.java
│   └── resources
└── test
    ├── java
    │   └── in
    │       └── one2n
    │           └── exercise
    │               └── GraderTest.java
    └── resources
        └── grades.csv

12 directories, 5 files

Assumptions:

  • Your program behaviour should be the same as thetree command.

  • The output is printed on STDOUT.

  • Use pipe (|) and dashes (--) to indent vertically and horizontally.

  • The tree utility is packaged as a standalone binary (./tree). Based on the programming language you're using, you may be able to create a standalone utility (e.g., in Go). If not, use any CLI execution method in your language (e.g. java -jar tree.jar for Java)

Expectations:

  • Write test cases for empty directories, nested empty directories, directories with multiple files, etc.

  • Your code should handle errors when there is no directory read (or execute) permission for nested directories.

Story 2 (listing option)

Print the relative path to the directory being searched.

➜  student-grading-java git:(main) tree -f src
src
├── src/main
│   ├── src/main/java
│   │   └── src/main/java/in
│   │       └── src/main/java/in/one2n
│   │           └── src/main/java/in/one2n/exercise
│   │               ├── src/main/java/in/one2n/exercise/Grade.java
│   │               ├── src/main/java/in/one2n/exercise/Grader.java
│   │               └── src/main/java/in/one2n/exercise/Student.java
│   └── src/main/resources
└── src/test
    ├── src/test/java
    │   └── src/test/java/in
    │       └── src/test/java/in/one2n
    │           └── src/test/java/in/one2n/exercise
    │               └── src/test/java/in/one2n/exercise/GraderTest.java
    └── src/test/resources
        └── src/test/resources/grades.csv

12 directories, 5 files

Expectations:

  • Handle argument parsing in the code

  • Unit tests

  • Can you reuse the code and design from the previous story? How will you refactor your existing code to make this code reuse possible?

Story 3 (listing option)

Only print directories, not files.

➜  student-grading-java git:(main) tree -d src
src
├── main
│   ├── java
│   │   └── in
│   │       └── one2n
│   │           └── exercise
│   └── resources
└── test
    ├── java
    │   └── in
    │       └── one2n
    │           └── exercise
    └── resources

12 directories

Expectations:

  • Reuse code from previous stories as much as possible. Make your code modular and extensible.

  • Write test cases for this story.

Story 4 (listing option)

Allow traversing specified nested levels only.
➜  student-grading-java git:(main) tree -L 3 src
src
├── main
│   ├── java
│   │   └── in
│   └── resources
└── test
    ├── java
    │   └── in
    └── resources
        └── grades.csv

8 directories, 1 file

Expectations:

  • As mentioned in previous stories, reuse code as much as possible.

  • Unit tests

Story 5 (file option)

Print file permissions for all files.

➜  student-grading-java git:(main) tree -p src
src
├── [drwxr-xr-x]  main
│   ├── [drwxr-xr-x]  java
│   │   └── [drwxr-xr-x]  in
│   │       └── [drwxr-xr-x]  one2n
│   │           └── [drwxr-xr-x]  exercise
│   │               ├── [-rw-r--r--]  Grade.java
│   │               ├── [-rw-r--r--]  Grader.java
│   │               └── [-rw-r--r--]  Student.java
│   └── [drwxr-xr-x]  resources
└── [drwxr-xr-x]  test
    ├── [drwxr-xr-x]  java
    │   └── [drwxr-xr-x]  in
    │       └── [drwxr-xr-x]  one2n
    │           └── [drwxr-xr-x]  exercise
    │               └── [-rw-r--r--]  GraderTest.java
    └── [drwxr-xr-x]  resources
        └── [-rw-r--r--]  grades.csv

12 directories, 5 files

Story 6 (sorting option)

Sort the output by the last modification time instead of alphabetically (the default). Note the actual output is just for indication only.

➜  student-grading-java git:(main) ✗ tree -t src
src
├── main
│   ├── java
│   │   └── in
│   │       └── one2n
│   │           └── exercise
│   │               ├── Grade.java
│   │               ├── Grader.java
│   │               └── Student.java
│   └── resources
└── test
    ├── java
    │   └── in
    │       └── one2n
    │           └── exercise
    │               └── GraderTest.java
    └── resources
        └── grades.csv

12 directories, 5 files

Story 7 (XML/JSON option)

Print output in the xml format.

  • X Turn on XML output. Outputs the directory tree as an XML formatted file.

  • J Turn on JSON output. Outputs the directory tree as a JSON formatted array.

➜  student-grading-java git:(main) ✗ tree -X -L 4 src
<?xml version="1.0" encoding="UTF-8"?>
<tree>
  <directory name="src">
    <directory name="main">
      <directory name="java">
        <directory name="in">
          <directory name="one2n">
          </directory>
        </directory>
      </directory>
      <directory name="resources">
      </directory>
    </directory>
    <directory name="test">
      <directory name="java">
        <directory name="in">
          <directory name="one2n">
          </directory>
        </directory>
      </directory>
      <directory name="resources">
        <file name="grades.csv"></file>
      </directory>
    </directory>
  </directory>
  <report>
    <directories>10</directories>
    <files>1</files>
  </report>
</tree

Story 8 (graphics option)

Do not print the indentation lines, typically used in conjunction with the -f option. Also, remove as much whitespace as possible when used with the -J or -x options.

➜  student-grading-java git:(main) ✗ tree -if src
src
src/main
src/main/java
src/main/java/in
src/main/java/in/one2n
src/main/java/in/one2n/exercise
src/main/java/in/one2n/exercise/Grade.java
src/main/java/in/one2n/exercise/Grader.java
src/main/java/in/one2n/exercise/Student.java
src/main/resources
src/test
src/test/java
src/test/java/in
src/test/java/in/one2n
src/test/java/in/one2n/exercise
src/test/java/in/one2n/exercise/GraderTest.java
src/test/resources
src/test/resources/grades.csv

12 directories, 5 files

Feel free to make suitable assumptions if needed, and ensure to document them in README.md