Airbnb questions

Cloud Storage System

AirbnbOnline assessmentHard

You are building an in-memory cloud storage system as the class CloudStorage. A file has a unique name and a size.

Part 1: File operations

Implement:

  • add_file(name, size) adds a file and returns true, or returns false without changing anything when a file with that name already exists.
  • get_file_size(name) returns the file's size, or null when there is no such file.
  • delete_file(name) removes the file and returns the size it had, or returns null when there is no such file.
  1. Example 1
    init
    []
    operations
    [["add_file",["a.txt",10]],["add_file",["b.txt",5]],["add_file",["a.txt",99]],["get_file_size",["a.txt"]],["get_file_size",["zzz"]],["delete_file",["a.txt"]],["delete_file",["a.txt"]],["get_file_size",["a.txt"]],["add_file",["a.txt",7]],["get_file_size",["a.txt"]]]
    Output
    [true,true,false,10,null,10,null,null,true,7]

    Why: The second add of a.txt fails because the name exists. Reading a missing file gives null. Deleting a.txt returns its size 10; deleting it again gives null, and the name can be added again with a new size.

Constraints

  • 0 <= size <= 10^9
  • 1 <= len(name) <= 100, and a name uses ASCII letters, digits and . / _ -
  • At most 10^4 calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Cloud Storage System: Airbnb interview question | LoopCoach