Feature #11: Directory Iterator

Implementing the "Directory Iterator" feature for our "Operating System" project.


In this feature, we will create a directory tree iterator. A directory can contain files or other directories. Similarly, subdirectories can contain both files and other directories. We will be given a directory structure for a specific directory in the file system. This directory will be available as a list. Each element of this list is either a file represented as a scalar element, or a directory represented as a nested list. We will have to iterate over all of the files one by one, using an iterator.

The task is to implement the NestedIterator module:

  • init(nested_list) initializes the iterator with the nested_list.
  • next() returns the next file in the nested directories.
  • has_next() returns true if there are still some files in the nested list. If there are no files left in the nested list, it returns false.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.