AlgoMaster Logo
AlgoMasterDesign File Treeeasy

Design File Tree

easy

A file answers questions about itself: it has one file, its own size, and a depth of one. A folder answers the same questions by asking every entry it contains. Because a folder may contain more folders, that one rule handles an entire directory tree.

The starter code already includes FileTree, including id management and insertion. Implement only the three Composite participants:

  • FileEntry, the common contract for files and folders.
  • FileLeaf, the leaf that represents one file.
  • FolderGroup, the composite that stores child entries.

The provided FileTree facade behaves as follows:

  • FileTree(String rootName) creates an empty root folder with id 0.
  • int addFile(int parentId, String name, int size) adds a file under a folder and returns its id.
  • int addFolder(int parentId, String name) adds a folder under a folder and returns its id.
  • Both add methods return -1 when the parent id is missing or belongs to a file. A failed add consumes no id.
  • String render() returns the whole tree. Folders end in /; files show their size as name (n KB). Each level adds two spaces and there is no trailing newline.
  • int fileCount() counts files anywhere in the tree, int totalSize() sums their sizes, and int depth() returns the number of levels. An empty root has depth 1.

The tests call FileTree, which delegates rendering and aggregation to the component classes. Do not reimplement the facade.

Example 1:

Input:

Output:

Explanation: The root contains one file and a folder. Following src and then tests reaches the deepest file at level four, while all three file sizes contribute to the total.

Example 2:

Input:

Output:

Explanation: notes.txt is a file, so it cannot accept bad.txt as a child. Parent id 9 is also missing. Neither failed insertion appears in the tree.

Constraints

  • -1 <= parentId <= 200
  • 0 <= size <= 100000
  • Names contain letters, digits, spaces, periods, underscores, and hyphens only.
  • At most 100 calls are made across all methods.

Starter Code

Only the three Composite participants are unfinished. Implement the shared component contract, the file leaf, and the folder composite at the marked locations. FileTree is already implemented and should not be rewritten.

How the design is graded

needs 7/10 to pass
  • One contract for files and folders

    Full marks when files and folders implement the same entry interface and a folder keeps one list of that interface. Lose points heavily when files and folders are stored in separate collections or recursive methods branch on concrete child types.

  • Recursive folder aggregation

    Full marks when rendering, file counting, size summing, and depth all delegate to every child, so folders nested four or more levels deep work without special cases. Lose points heavily when only direct children are included.

  • Leaf and empty-folder behavior

    Full marks when a file contributes count 1, its own size, and depth 1; an empty folder contributes count 0, size 0, and depth 1; and rendering uses exactly two spaces per level with no trailing newline. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new FileTree("root")null
addFile(0, "readme.md", 4)1
addFolder(0, "src")2
addFile(2, "app.java", 12)3
addFolder(2, "tests")4
addFile(4, "app_test.java", 8)5
render()"root/\n readme.md (4 KB)\n src/\n app.java (12 KB)\n tests/\n app_test.java (8 KB)"
fileCount()3
totalSize()24
depth()4

The root contains one file and a folder. Following src and then tests reaches the deepest file at level four, while all three file sizes contribute to the total.

Run checks these cases. Submit also runs a larger hidden set.