Design data structures and algorithms for in-memory file system Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 5 Likes Like Report Explain the data structures and algorithms that you would use to design an in-memory file system. Illustrate with an example in the code logic where possible. Asked In: Amazon A file system, in its most simplistic version, consists of Files and Directories. Each Directory contains a set of Files and Directories. Since Files and Directories share so many characteristics, we've implemented them such that they inherit from the same class, Entry. Implemented Main logic in Java Java // Entry is superclass for both File and Directory public abstract class Entry { protected Directory parent; protected long created; protected long lastUpdated; protected long lastAccessed; protected String name; public Entry(String n, Directory p) { name = n; parent = p; created= System.currentTimeMillis(); lastUpdated = System.currentTimeMillis(); lastAccessed = System.currentTimeMillis(); } public boolean delete() { if (parent == null) return false; return parent.deleteEntry(this); } public abstract int size(); /* Getters and setters. */ public long getcreationTime() { return created; } public long getLastUpdatedTime() { return lastUpdated; } public long getLastAccessedTime() { return lastAccessed; } public void changeName(String n) { name = n; } public String getName() { return name; } } // A class to represent a File (Inherits // from Entry) public class File extends Entry { private String content; private int size; public File(String n, Directory p, int sz) { super(n, p); size = sz; } public int size() { return size; } public String getContents() { return content; } public void setContents(String c) { content = c; } } // A class to represent a Directory (Inherits // from Entry) public class Directory extends Entry { protected Arraylist<Entry> contents; public Directory(String n, Directory p) { super(n, p); contents = new Arraylist<Entry>(); } public int size() { int size = 0; for (Entry e : contents) size += e.size(); return size; } public int numberOfFiles() { int count = 0; for (Entry e : contents) { if (e instanceof Directory) { count++; // Directory counts as a file Directory d = (Directory) e; count += d. numberOfFiles (); } else if (e instanceof File) count++; } return count; } public boolean deleteEntry(Entry entry) { return contents.remove(entry); } public void addEntry(Entry entry) { contents.add(entry); } protected ArrayList<Entry> getContents() { return contents; } } Alternatively, we could have implemented Directory such that it contains separate lists for files and subdirectories. This makes the nurnberOfFiles () method a bit cleaner, since it doesn't need to use the instanceof operator, but it does prohibit us from cleanly sorting files and directories by dates or names. For data block allocation, we can use bitmask vector and linear search (see “Practical File System Design”) or B+ trees (see Reference or Wikipedia). References: https://stackoverflow.com/questions/14126575/data-structures-used-to-build-file-systems This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Comment K kartik 5 Improve K kartik 5 Improve Article Tags : Design Pattern System Design Amazon Explore What is System DesignSystem Design Introduction - LLD & HLD7 min readSystem Design Life Cycle | SDLC (Design)7 min readWhat are the components of System Design?10 min readGoals and Objectives of System Design5 min readWhy is it Important to Learn System Design?6 min readImportant Key Concepts and Terminologies â Learn System Design9 min readAdvantages of System Design4 min readSystem Design FundamentalsAnalysis of Monolithic and Distributed Systems - Learn System Design10 min readRequirements Gathering in System Design6 min readDifferences between System Analysis and System Design4 min readHorizontal and Vertical Scaling | System Design5 min readCapacity Estimation in Systems Design10 min readHow to Answer a System Design Interview Problem/Question?5 min readFunctional and Non Functional Requirements5 min readWeb Server, Proxies and their role in Designing Systems9 min readScalability in System DesignWhat is Scalability and How to achieve it?7 min readWhich Scalability approach is right for our Application? - System Design4 min readPrimary Bottlenecks that Hurt the Scalability of an Application - System Design4 min readDatabases in Designing SystemsComplete Guide to Database Design - System Design11 min readSQL vs. NoSQL - Which Database to Choose in System Design?5 min readFile and Database Storage Systems in System Design4 min readBlock, Object, and File Storage in System Design6 min readDatabase Sharding - System Design8 min readDatabase Replication in System Design6 min readHigh Level Design(HLD)What is High Level Design? - Learn System Design9 min readAvailability in System Design5 min readConsistency in System Design8 min readReliability in System Design5 min readCAP Theorem in System Design5 min readWhat is API Gateway?7 min readWhat is Content Delivery Network(CDN) in System Design7 min readWhat is Load Balancer & How Load Balancing works?4 min readCaching - System Design Concept8 min readCommunication Protocols in System Design6 min readActivity Diagrams - Unified Modeling Language (UML)10 min readMessage Queues - System Design12 min readLow Level Design(LLD)What is Low Level Design or LLD?6 min readAuthentication vs Authorization in LLD - System Design3 min readPerformance Optimization Techniques for System Design3 min readObject-Oriented Analysis and Design(OOAD)6 min readData Structures and Algorithms for System Design6 min readContainerization Architecture in System Design10 min readModularity and Interfaces In System Design8 min readUnified Modeling Language (UML) Diagrams8 min readData Partitioning Techniques in System Design5 min readHow to Prepare for Low-Level Design Interviews?4 min readEssential Security Measures in System Design8 min readDesign PatternsDesign Patterns Tutorial9 min readCreational Design Patterns4 min readStructural Design Patterns7 min readBehavioral Design Patterns5 min readDesign Patterns Cheat Sheet - When to Use Which Design Pattern?7 min readInterview Guide for System DesignHow to Crack System Design Interview Round?9 min readSystem Design Interview Questions and Answers1 min read5 Common System Design Concepts for Interview Preparation12 min read5 Tips to Crack Low-Level System Design Interviews6 min readSystem Design Interview Questions & AnswersMost Commonly Asked System Design Interview Problems/Questions1 min readDesign Dropbox - A System Design Interview Question14 min readDesigning Twitter - A System Design Interview Question15+ min readSystem Design Netflix | A Complete Architecture14 min readSystem Design of Uber App | Uber System Architecture13 min readDesign BookMyShow - A System Design Interview Question10 min readDesigning Facebook Messenger | System Design Interview9 min readComplete Roadmap to Learn System Design for Beginners6 min readGuide to System Design for Freshers15+ min readHow Disney+ Hotstar Managed (5 Cr)+ Live Viewers During India's T20 World Cup Win[2024]8 min read Like