Level: Senior-Level
Round: Online Assessment · Type: Coding · Difficulty: 7/10 · Duration: 60 min · Interviewer: Neutral
Topics: Data Structures, Hash Table, Design
Location: San Francisco Bay Area
Interview date: 2025-12-28
Implement a simple cloud storage service that allows users to store files, duplicate existing files, and query file sizes.
Implement a simple cloud storage service that allows users to store files, duplicate existing files, and query file sizes.
The system should support the following operations:
boolean addFile(String name, int size)
Adds a new file to the storage system.
name represents the file name.size specifies the file size in bytes.Rules:
boolean copyFile(String nameFrom, String nameTo)
Creates a copy of an existing file.
nameFrom should be duplicated and stored under the name nameTo.Rules:
nameFrom does not correspond to an existing file or refers to a directory.nameTo.int getFilesize(String name)
Returns the size of the specified file.
Rules:
name is a non-empty string and may contain any visible ASCII characters, including /.` Input: ["InMemoryDB", "setData", "setData", "getData", "getData", "deleteData", "deleteData"] [[], ["A", "B", "E"], ["A", "C", "F"], ["A", "B"], ["A", "D"], ["A", "B"], ["A", "D"]]
Output: [null, null, null, "E", "", true, false]
Explanation:
InMemoryDB db = new InMemoryDB(); db.setData("A", "B", "E"); // Returns null. Database state: {"A": {"B": "E"}} db.setData("A", "C", "F"); // Returns null. Database state: {"A": {"C": "F", "B": "E"}} db.getData("A", "B"); // Returns "E". db.getData("A", "D"); // Returns "", since there is no value of field "D". db.deleteData("A", "B"); // Returns true. Database state: {"A": {"C": "F"}} db.deleteData("A", "D"); // Returns false. Database state: {"A": {"c": "F"}} `
` from typing import List, Optional
class InMemoryDB: def init(self): # The main map: key -> (field -> value) self.db = {}
def setData(self, key: str, field: str, value: str) -> None:
if key not in self.db:
self.db[key] = {}
self.db[key][field] = value
def getData(self, key: str, field: str) -> str:
if key not in self.db:
return ""
fields = self.db[key]
value = fields.get(field)
return value if value is not None else ""
def deleteData(self, key: str, field: str) -> bool:
if key not in self.db:
return False
fields = self.db[key]
if field in fields:
del fields[field]
# If the record is empty after removal, remove the key entirely
if not fields:
del self.db[key]
return True
return False
`