Skip to content

2408. Design SQL 👎

  • Time:
    • Constructor: $O(1)$
    • insertRow(name: str, row: list[str]): $O(1)$
    • deleteRow(name: str, rowId: int): $O(1)$
    • selectCell(name: str, rowId: int, columnId: int): $O(1)$
  • Space: $O(|\texttt{insertRow()}|)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class SQL {
 public:
  void insertRow(string name, vector<string> row) {
    db[name].push_back(row);
  }

  void deleteRow(string name, int rowId) {}

  string selectCell(string name, int rowId, int columnId) {
    return db[name][rowId - 1][columnId - 1];
  }

 private:
  unordered_map<string, vector<vector<string>>> db;
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class SQL {
  public SQL(List<String> names, List<Integer> columns) {}

  public void insertRow(String name, List<String> row) {
    db.putIfAbsent(name, new ArrayList<>());
    db.get(name).add(row);
  }

  public void deleteRow(String name, int rowId) {}

  public String selectCell(String name, int rowId, int columnId) {
    return db.get(name).get(rowId - 1).get(columnId - 1);
  }

  private Map<String, List<List<String>>> db = new HashMap<>();
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class SQL:
  def __init__(self, names: list[str], columns: list[int]):
    self.db: dict[str, list[list[str]]] = collections.defaultdict(list)

  def insertRow(self, name: str, row: list[str]) -> None:
    self.db[name].append(row)

  def deleteRow(self, name: str, rowId: int) -> None:
    pass

  def selectCell(self, name: str, rowId: int, columnId: int) -> str:
    return self.db[name][rowId - 1][columnId - 1]