Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1500: Design a File Sharing System

In this guide, we solve Leetcode #1500 Design a File Sharing System in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

We will use 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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Design, Hash Table, Data Stream, Sorting, Heap (Priority Queue)

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

Example

Input: ["FileSharing","join","join","join","request","request","leave","request","leave","join"] [[4],[[1,2]],[[2,3]],[[4]],[1,3],[2,2],[1],[2,1],[2],[[]]] Output: [null,1,2,3,[2],[1,2],null,[],null,1] Explanation: FileSharing fileSharing = new FileSharing(4); // We use the system to share a file of 4 chunks. fileSharing.join([1, 2]); // A user who has chunks [1,2] joined the system, assign id = 1 to them and return 1. fileSharing.join([2, 3]); // A user who has chunks [2,3] joined the system, assign id = 2 to them and return 2. fileSharing.join([4]); // A user who has chunk [4] joined the system, assign id = 3 to them and return 3. fileSharing.request(1, 3); // The user with id = 1 requested the third file chunk, as only the user with id = 2 has the file, return [2] . Notice that user 1 now has chunks [1,2,3]. fileSharing.request(2, 2); // The user with id = 2 requested the second file chunk, users with ids [1,2] have this chunk, thus we return [1,2]. fileSharing.leave(1); // The user with id = 1 left the system, all the file chunks with them are no longer available for other users. fileSharing.request(2, 1); // The user with id = 2 requested the first file chunk, no one in the system has this chunk, we return empty list []. fileSharing.leave(2); // The user with id = 2 left the system. fileSharing.join([]); // A user who doesn't have any chunks joined the system, assign id = 1 to them and return 1. Notice that ids 1 and 2 are free and we can reuse them.

Python Solution

class FileSharing: def __init__(self, m: int): self.cur = 0 self.chunks = m self.reused = [] self.user_chunks = defaultdict(set) def join(self, ownedChunks: List[int]) -> int: if self.reused: userID = heappop(self.reused) else: self.cur += 1 userID = self.cur self.user_chunks[userID] = set(ownedChunks) return userID def leave(self, userID: int) -> None: heappush(self.reused, userID) self.user_chunks.pop(userID) def request(self, userID: int, chunkID: int) -> List[int]: if chunkID < 1 or chunkID > self.chunks: return [] res = [] for k, v in self.user_chunks.items(): if chunkID in v: res.append(k) if res: self.user_chunks[userID].add(chunkID) return sorted(res) # Your FileSharing object will be instantiated and called as such: # obj = FileSharing(m) # param_1 = obj.join(ownedChunks) # obj.leave(userID) # param_3 = obj.request(userID,chunkID)

Complexity

The time complexity is O(n). The space complexity is O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy