Start learning free

LESSON 341 OF 343 · OBJECT-ORIENTED DESIGN - INTERVIEWS

In-Memory File System

Practice O(depth)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build a small file system that lives in memory. It holds folders and files. Each file has a size in bytes.

A path says where something is. The root folder is "/". The path "/docs/notes" is the folder notes, inside the folder docs, inside the root.

Implement two classes:

  • Node(name, isFile, size, parent) is one file or one folder. It keeps its name and its size. It also knows whether it is a file, its parent folder and its children by name.
  • FileSystem() starts with only the root folder.
  • mkdir(path) makes a folder at path. It returns true if it did.
  • addFile(path, size) makes a file of size bytes at path. It returns true if it did.
  • ls(path) returns the names inside the folder at path, from A to Z.
  • size(path) returns how many bytes are at path.
  • remove(path) removes the file or the folder at path. It returns true if it did.

In Swift, Node is a class, not a struct. So a folder and its children share the same nodes.

The tests call only FileSystem. Its methods build the tree out of nodes.

A new file or folder needs two things. Its parent must be an existing folder. And nothing may be at its path yet. Otherwise mkdir and addFile return false and change nothing. Missing parent folders are never made for you.

A file holds nothing. So a path that goes through a file has no parent folder.

For a file, ls returns a list with just the file's name. When nothing is at the path, it returns an empty list.

The size of a file is its own size. The size of a folder is the total of all the files inside it, at any depth. An empty folder has size 0. When nothing is at the path, size returns -1.

Removing a folder removes everything inside it too. remove returns false when nothing is at the path. The root can't be removed.

The calls run in order on one new FileSystem. A call to mkdir, addFile or remove writes True or False. A call to size writes the number. A call to ls writes how many names it returned on one line. Then it writes each name on its own line.

 

Example 1:

Input:
FileSystem()
1. mkdir("/docs")
2. mkdir("/docs/notes")
3. addFile("/docs/notes/todo", 120)
4. addFile("/docs/plan", 300)
5. ls("/docs")
6. ls("/docs/plan")
7. size("/docs/notes")
8. size("/docs")
9. size("/docs/plan")
Output:
True
True
True
True
2
notes
plan
1
plan
120
420
300
Explanation:
Calls 1 and 2 make the folders docs and notes.
Call 3 puts a file of 120 bytes, todo, in notes.
Call 4 puts a file of 300 bytes, plan, in docs.
Call 5 lists docs from A to Z: notes, then plan.
Call 6 lists a file, so it returns just its name.
The folder docs holds 120 + 300 = 420 bytes.

Example 2:

Input:
FileSystem()
1. mkdir("/photos/trip")
2. mkdir("/photos")
3. mkdir("/photos")
4. addFile("/photos/cat", 50)
5. addFile("/photos/cat", 80)
6. mkdir("/photos/cat/old")
7. mkdir("/")
8. ls("/photos/trip")
9. size("/photos/cat/old")
10. size("/photos")
Output:
False
True
False
True
False
False
False
0
-1
50
Explanation:
Call 1 fails, because the folder photos doesn't exist yet.
Call 3 fails, because photos is already there.
Call 5 fails too. So cat keeps its 50 bytes.
Call 6 fails, because cat is a file. A file holds nothing.
Call 7 fails, because the root folder is always there.
Nothing is at the paths of calls 8 and 9.

Example 3:

Input:
FileSystem()
1. mkdir("/music")
2. mkdir("/music/rock")
3. addFile("/music/rock/song", 400)
4. addFile("/music/intro", 100)
5. addFile("/readme", 7)
6. size("/")
7. remove("/music/rock")
8. size("/music")
9. ls("/music")
10. remove("/music/rock")
11. remove("/")
12. size("/")
Output:
True
True
True
True
True
507
True
100
1
intro
False
False
107
Explanation:
Before call 6, the files hold 400 + 100 + 7 = 507 bytes.
Call 7 removes rock and the song inside it.
So music holds only intro, with its 100 bytes.
Call 10 fails, because rock is gone.
Call 11 fails, because the root can't be removed.
Only intro and readme are left: 100 + 7 = 107.

 

Constraints:

  • A path is "/" or a list of names, each one after a "/".
  • A name has 1 to 10 lowercase English letters.
  • A path has at most 20 names.
  • A path never ends with "/", except the root "/".
  • Each size given to addFile is a whole number from 1 to 10,000.
  • At most 100,000 calls follow the constructor.
  • At most 20,000 files exist at the same time.

Note:

Each folder should keep the total size of the files inside it. Adding or removing a file or a folder then changes that total on every folder up its path. That takes O(d) time for a path of d names. Then size only has to find the node, also in O(d). A call to ls sorts the k names of a folder, in O(k log k). A small private helper walks a path to its node, or to nothing. Adding up a whole folder on every size call would be too slow.

STEP 1 OF 36
    Ctrl + Enter to check