Design an in-memory cloud storage service with per-user quotas, LRU eviction and compression

Quick Overview

Design an in-memory cloud storage service with per-user quotas, where an upload evicts the user's least recently used files when space runs out and each file can be compressed once. It tests LRU bookkeeping with a hash map and an ordered list, exact usage accounting, and clear failure codes.

Design an in-memory cloud storage service with per-user quotas, LRU eviction and compression

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory cloud storage service that supports multiple users, each with a per-user storage quota. Users can upload files with a given size, retrieve files, delete files, and compress existing files to reduce their storage footprint. When a user uploads a file that would push them over their quota, the system automatically evicts that user's least recently used (LRU) files until there is enough space. If the file would not fit even after evicting every existing file, the upload fails. A failed upload changes nothing: no file is evicted, and an existing file with the same name is kept. Retrieving or compressing a file counts as a use and makes it the most recently used file, and a newly uploaded file (including a replacement) starts as the most recently used. Compression halves the file size using integer division (`new_size = old_size // 2`), and a file that is already compressed cannot be compressed again. Each user's quota and files are independent of every other user's. Implement these methods: - `add_user(user_id: str, quota: int) -> bool` registers a new user with the given storage quota. Return `True` if the user was added, or `False` if the user already exists. - `upload(user_id: str, file_name: str, file_size: int) -> bool` uploads a file for the user. If a file with that name already exists, it is replaced: the old version's space is freed first, and the new version starts uncompressed. If the upload would exceed the quota, LRU files are evicted until it fits. Return `True` on success, or `False` if the user does not exist or the file is too large even after evicting all files. - `get(user_id: str, file_name: str) -> int` returns the file's size and marks it as most recently used, or returns `-1` if the user or file is not found. - `delete(user_id: str, file_name: str) -> bool` deletes the file. Return `True` if it was deleted, or `False` if the user or file is not found. - `compress(user_id: str, file_name: str) -> int` halves the file's size with integer division and marks it as most recently used. Return the new size, `-1` if the user or file is not found, or `-2` if the file is already compressed. - `get_usage(user_id: str) -> int` returns the user's current total storage usage, or `-1` if the user does not exist. For example: ```text add_user("u1", 100) -> True add_user("u1", 50) -> False u1 already exists upload("u1", "a.txt", 40) -> True usage 40 upload("u1", "b.txt", 30) -> True usage 70 get("u1", "a.txt") -> 40 a.txt is now the most recently used, b.txt the least upload("u1", "c.txt", 50) -> True 70 + 50 > 100, so b.txt is evicted; usage 40 + 50 = 90 get("u1", "b.txt") -> -1 b.txt was evicted get_usage("u1") -> 90 ``` Compression, a failed upload and a replacement: ```text add_user("u2", 10) -> True upload("u2", "x.bin", 7) -> True usage 7 compress("u2", "x.bin") -> 3 7 // 2 = 3; usage 3 compress("u2", "x.bin") -> -2 already compressed upload("u2", "y.bin", 6) -> True 3 + 6 = 9 fits; usage 9 upload("u2", "z.bin", 11) -> False larger than the quota even with every file evicted get_usage("u2") -> 9 the failed upload evicted nothing upload("u2", "x.bin", 8) -> True old x.bin (3) is freed, 6 + 8 > 10 so y.bin is evicted; usage 8 compress("u2", "x.bin") -> 4 the replacement is a new, uncompressed file; usage 4 delete("u2", "y.bin") -> False y.bin was evicted get("u3", "x.bin") -> -1 no such user get_usage("u3") -> -1 ``` ```hint Two questions per user For each user you need to find a file by name and also find the file used longest ago, and both have to stay cheap as files are touched. Think about which structure answers both in constant time. ``` ```hint Order of steps in upload Work out at which point an upload can already be known to fail, and what must still be untouched when it does. Then decide where freeing a replaced file's old version fits relative to eviction. ``` ### Constraints & Assumptions - `1 <= quota <= 10^9` and `1 <= file_size <= 10^9`. - User ids are non-empty strings. File names are non-empty strings of letters, digits, dots and underscores. - Compression halves the size with integer division, so a file of size 1 compresses to 0. - At most `10^4` operations in total. - One user's upload never evicts another user's files. ### Clarifying Questions to Ask - Do failed calls, such as a `get` of a missing file or a `compress` that returns `-2`, change the LRU order? - Is the quota fixed after `add_user`, or can it change later? - Can a compressed file be decompressed later, and does the service need to remember its original size? ### What a Strong Answer Covers - A per-user structure that gives constant-time lookup by name and constant-time access to the least recently used file, with the order updated on upload, `get` and `compress`. - An upload path that rejects a file larger than the quota before touching anything, frees the old version on replacement, and then evicts from the LRU end only as far as needed. - A running usage total that stays exact through uploads, replacements, evictions, deletes and compression. - Every return code: `-1` for a missing user or file, `-2` for a second compression, and `False` for an unknown user or an oversized upload. - The cost of each operation, including why eviction is cheap when averaged over all calls. ### Follow-up Questions - How would you add `update_quota(user_id, new_quota)` that evicts files when the quota shrinks below the current usage? - How would you support `decompress`, and what should happen when the restored size no longer fits in the quota? - How would the design change if eviction had to pick the largest file first instead of the least recently used one? - If the service ran on several servers, how would you keep each user's usage and LRU order consistent?

Overview: Design an in-memory cloud storage service with per-user quotas, where an upload evicts the user's least recently used files when space runs out and each file can be compressed once. It tests LRU bookkeeping with a hash map and an ordered list, exact usage accounting, and clear failure codes.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory cloud storage service that supports multiple users, each with a per-user storage quota. Users can upload files with a given size, retrieve files, delete files, and compress existing files to reduce their storage footprint.

When a user uploads a file that would push them over their quota, the system automatically evicts that user's least recently used (LRU) files until there is enough space. If the file would not fit even after evicting every existing file, the upload fails. A failed upload changes nothing: no file is evicted, and an existing file with the same name is kept.

Retrieving or compressing a file counts as a use and makes it the most recently used file, and a newly uploaded file (including a replacement) starts as the most recently used. Compression halves the file size using integer division (new_size = old_size // 2), and a file that is already compressed cannot be compressed again. Each user's quota and files are independent of every other user's.

Implement these methods:

  • add_user(user_id: str, quota: int) -> bool registers a new user with the given storage quota. Return True if the user was added, or False if the user already exists.
  • upload(user_id: str, file_name: str, file_size: int) -> bool uploads a file for the user. If a file with that name already exists, it is replaced: the old version's space is freed first, and the new version starts uncompressed. If the upload would exceed the quota, LRU files are evicted until it fits. Return True on success, or False if the user does not exist or the file is too large even after evicting all files.
  • get(user_id: str, file_name: str) -> int returns the file's size and marks it as most recently used, or returns -1 if the user or file is not found.
  • delete(user_id: str, file_name: str) -> bool deletes the file. Return True if it was deleted, or False if the user or file is not found.
  • compress(user_id: str, file_name: str) -> int halves the file's size with integer division and marks it as most recently used. Return the new size, -1 if the user or file is not found, or -2 if the file is already compressed.
  • get_usage(user_id: str) -> int returns the user's current total storage usage, or -1 if the user does not exist.

For example:

add_user("u1", 100)            -> True
add_user("u1", 50)             -> False   u1 already exists
upload("u1", "a.txt", 40)      -> True    usage 40
upload("u1", "b.txt", 30)      -> True    usage 70
get("u1", "a.txt")             -> 40      a.txt is now the most recently used, b.txt the least
upload("u1", "c.txt", 50)      -> True    70 + 50 > 100, so b.txt is evicted; usage 40 + 50 = 90
get("u1", "b.txt")             -> -1      b.txt was evicted
get_usage("u1")                -> 90

Compression, a failed upload and a replacement:

add_user("u2", 10)             -> True
upload("u2", "x.bin", 7)       -> True    usage 7
compress("u2", "x.bin")        -> 3       7 // 2 = 3; usage 3
compress("u2", "x.bin")        -> -2      already compressed
upload("u2", "y.bin", 6)       -> True    3 + 6 = 9 fits; usage 9
upload("u2", "z.bin", 11)      -> False   larger than the quota even with every file evicted
get_usage("u2")                -> 9       the failed upload evicted nothing
upload("u2", "x.bin", 8)       -> True    old x.bin (3) is freed, 6 + 8 > 10 so y.bin is evicted; usage 8
compress("u2", "x.bin")        -> 4       the replacement is a new, uncompressed file; usage 4
delete("u2", "y.bin")          -> False   y.bin was evicted
get("u3", "x.bin")             -> -1      no such user
get_usage("u3")                -> -1

Constraints & Assumptions

  • 1 <= quota <= 10^9 and 1 <= file_size <= 10^9 .
  • User ids are non-empty strings. File names are non-empty strings of letters, digits, dots and underscores.
  • Compression halves the size with integer division, so a file of size 1 compresses to 0.
  • At most 10^4 operations in total.
  • One user's upload never evicts another user's files.

Clarifying Questions to Ask Guidance

  • Do failed calls, such as a get of a missing file or a compress that returns -2 , change the LRU order?
  • Is the quota fixed after add_user , or can it change later?
  • Can a compressed file be decompressed later, and does the service need to remember its original size?

What a Strong Answer Covers Guidance

  • A per-user structure that gives constant-time lookup by name and constant-time access to the least recently used file, with the order updated on upload, get and compress .
  • An upload path that rejects a file larger than the quota before touching anything, frees the old version on replacement, and then evicts from the LRU end only as far as needed.
  • A running usage total that stays exact through uploads, replacements, evictions, deletes and compression.
  • Every return code: -1 for a missing user or file, -2 for a second compression, and False for an unknown user or an oversized upload.
  • The cost of each operation, including why eviction is cheap when averaged over all calls.

Follow-up Questions Guidance

  • How would you add update_quota(user_id, new_quota) that evicts files when the quota shrinks below the current usage?
  • How would you support decompress , and what should happen when the restored size no longer fits in the quota?
  • How would the design change if eviction had to pick the largest file first instead of the least recently used one?
  • If the service ran on several servers, how would you keep each user's usage and LRU order consistent?