Atlassian P60 Phone Screen | Cleared and moved to Onsite
Anonymous User
3009

Interviewer asked to use your own IDE or HackerRank coding tool. Then, he went on asking below question:

Imagine we have a system that stores files, and these files can be grouped into collections. We are interested in knowing where our resources are being taken up.
For this system we would like to generate a report that lists:
- The total size of all files stored;
- The top N collections (by file size) where N can be a user-defined value

An example input into your report generator might look like:
file1.txt (size: 100)
file2.txt (size: 200) in collection "collection1"
file3.txt (size: 200) in collection "collection1"
file4.txt (size: 300) in collection "collection2"
file5.txt (size: 10)

So, basically collections are like tags and files can be tagged. We have to implement methods that can return total size of all the files added to the system and topN collections. TopN collections is based on total size of all the files tagged with particular collection. In above example "collection1" has 2 files with total size 400, "collection2" has 1 file with total size 300 and there are 2 files without any collection with total size 110.
Top2 collections would return ["collection1", "collection2"]
I approached with creating a File Object
class File {
	String name;
	int size;
	String collection;
}

class FileCollection {
	String collection;
	int totalSizeInCollection;
}

class MemoryFile {
	PriorityQueue<FileCollection> queue;
	Map<String, List<File>> map;
	int totalSize;
	
	public MemoryFile() {
		this.queue = new PriorityQueue<FileCollection>((a, b) -> a.totalSizeInCollection - b.totalSizeInCollection);
		this.map = new HashMap<>();
	}
	
	public int totalSize() {
		return this.totalSize;
	}
	
	public void addFile(File f) {
		// check in map where key -> collection, value -> list of all files belonging to this collection
		// keep in mind, files without collection can be mapped to "default" collection (I basically took this approach and explained to interviewer and he was impressed :))
		// increase the totalSize here as well.
	}
	
	public List<String> topN(int n) {
		// traverse through map and create a FileCollection object and add to priorityQueue.
		// if count is greater than n, poll from the queue and in the end we will have only N elements in the
		// queue. Build the List<String> from the queue and return.
	}
}
Comments (6)