Design a File Sharing System - Problem

Design a file-sharing system to share a very large file which consists of m small chunks with IDs from 1 to m.

When users join the system, the system should assign a unique ID to them. The unique ID should be used once for each user, but when a user leaves the system, the ID can be reused again.

Users can request a certain chunk of the file, the system should return a list of IDs of all the users who own this chunk. If the user receives a non-empty list of IDs, they receive the requested chunk successfully.

Implement the FileSharing class:

  • FileSharing(int m) Initializes the object with a file of m chunks.
  • int join(int[] ownedChunks): A new user joined the system owning some chunks of the file, the system should assign an id to the user which is the smallest positive integer not taken by any other user. Return the assigned id.
  • void leave(int userID): The user with userID will leave the system, you cannot take file chunks from them anymore.
  • int[] request(int userID, int chunkID): The user userID requested the file chunk with chunkID. Return a list of the IDs of all users that own this chunk sorted in ascending order.

Input & Output

Example 1 — Basic Operations
$ Input: FileSharing(4), join([1,2]), join([2,3]), request(1,2), leave(1), join([4])
Output: [null,1,2,[1,2],null,1]
💡 Note: Initialize with 4 chunks. User 1 joins with chunks [1,2], gets ID 1. User 2 joins with chunks [2,3], gets ID 2. User 1 requests chunk 2, returns [1,2] (both users have it). User 1 leaves, ID 1 becomes available. New user joins with chunk [4], gets reused ID 1.
Example 2 — ID Reuse
$ Input: FileSharing(3), join([1]), join([2]), leave(1), join([3])
Output: [null,1,2,null,1]
💡 Note: Two users join and get IDs 1,2. User 1 leaves, making ID 1 available. Next user gets the smallest available ID which is 1 (reused).
Example 3 — Empty Request
$ Input: FileSharing(2), join([1]), request(1,2)
Output: [null,1,[]]
💡 Note: User 1 has chunk 1 but requests chunk 2. Since no one owns chunk 2, return empty list and user doesn't get the chunk.

Constraints

  • 1 ≤ m ≤ 105
  • 0 ≤ ownedChunks.length ≤ m
  • 1 ≤ ownedChunks[i] ≤ m
  • Values of ownedChunks are unique
  • 1 ≤ chunkID ≤ m
  • userID is guaranteed to be a user in the system if you assign the ID to them
  • At most 104 calls will be made to join, leave and request

Visualization

Tap to expand
File Sharing System Design INPUT File with m=4 chunks C1 C2 C3 C4 Operations: 1. FileSharing(4) 2. join([1,2]) 3. join([2,3]) 4. request(1,2) 5. leave(1) 6. join([4]) Data: HashMap + Min-Heap for ID management HashMap Min-Heap ALGORITHM STEPS 1 Initialize System Set m=4, nextId=1 Empty heap, empty map 2 Join Operations Assign ID (heap or nextId++) Map user to chunks owned 3 Request Chunk Find all users with chunk Add chunk to requester 4 Leave/Rejoin Push ID to min-heap Reuse smallest ID State After Operations User1: [1,2] -- leaves User2: [2,3] User1(reused): [4] Min-Heap: [1] -- ID reused nextId: 3 FINAL RESULT Operation Results: FileSharing(4) null join([1,2]) 1 join([2,3]) 2 request(1,2) [1,2] leave(1) null join([4]) 1 (reused!) Output Array: [null,1,2,[1,2],null,1] OK - Verified Key Insight: The Min-Heap ensures the smallest available ID is always reused first when users leave and rejoin. HashMap provides O(1) lookup for user chunks. Chunk ownership tracking enables efficient request() operations by maintaining an inverted index from chunk IDs to user sets. TutorialsPoint - Design a File Sharing System | Hash Map with Min-Heap for ID Reuse
Asked in
Google 15 Amazon 12 Microsoft 8 Dropbox 10
18.2K Views
Medium Frequency
~35 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen