Design SQL Problem
Description
LeetCode Problem 2408.
You are given two string arrays, names and columns, both of size n. The i^th table is represented by the name names[i] and contains columns[i] number of columns.
You need to implement a class that supports the following operations:
- Insert a row in a specific table with an id assigned using an auto-increment method, where the id of the first inserted row is 1, and the id of each new row inserted into the same table is one greater than the id of the last inserted row, even if the last row was removed.
- Remove a row from a specific table. Removing a row does not affect the id of the next inserted row.
- Select a specific cell from any table and return its value.
- Export all rows from any table in csv format.
Implement the SQL class:
SQL(String[] names, int[] columns)- Creates the
ntables.
- Creates the
bool ins(String name, String[] row)- Inserts
rowinto the tablenameand returnstrue. - If
row.lengthdoes not match the expected number of columns, ornameis not a valid table, returnsfalsewithout any insertion.
- Inserts
void rmv(String name, int rowId)- Removes the row
rowIdfrom the tablename. - If
nameis not a valid table or there is no row with idrowId, no removal is performed.
- Removes the row
String sel(String name, int rowId, int columnId)- Returns the value of the cell at the specified
rowIdandcolumnIdin the tablename. - If
nameis not a valid table, or the cell(rowId, columnId)is invalid, returns"<null>".
- Returns the value of the cell at the specified
String[] exp(String name)- Returns the rows present in the table
name. - If name is not a valid table, returns an empty array. Each row is represented as a string, with each cell value (including the row's id) separated by a
",".
- Returns the rows present in the table
Example 1:
Input:
["SQL","ins","sel","ins","exp","rmv","sel","exp"]
[[["one","two","three"],[2,3,1]],["two",["first","second","third"]],["two",1,3],["two",["fourth","fifth","sixth"]],["two"],["two",1],["two",2,2],["two"]]
Output:
[null,true,"third",true,["1,first,second,third","2,fourth,fifth,sixth"],null,"fifth",["2,fourth,fifth,sixth"]]
Explanation:
// Creates three tables.
SQL sql = new SQL(["one", "two", "three"], [2, 3, 1]);
// Adds a row to the table "two" with id 1. Returns True.
sql.ins("two", ["first", "second", "third"]);
// Returns the value "third" from the third column
// in the row with id 1 of the table "two".
sql.sel("two", 1, 3);
// Adds another row to the table "two" with id 2. Returns True.
sql.ins("two", ["fourth", "fifth", "sixth"]);
// Exports the rows of the table "two".
// Currently, the table has 2 rows with ids 1 and 2.
sql.exp("two");
// Removes the first row of the table "two". Note that the second row
// will still have the id 2.
sql.rmv("two", 1);
// Returns the value "fifth" from the second column
// in the row with id 2 of the table "two".
sql.sel("two", 2, 2);
// Exports the rows of the table "two".
// Currently, the table has 1 row with id 2.
sql.exp("two");
Example 2:
Input:
["SQL","ins","sel","rmv","sel","ins","ins"]
[[["one","two","three"],[2,3,1]],["two",["first","second","third"]],["two",1,3],["two",1],["two",1,2],["two",["fourth","fifth"]],["two",["fourth","fifth","sixth"]]]
Output:
[null,true,"third",null,"<null>",false,true]
Explanation:
// Creates three tables.
SQL sQL = new SQL(["one", "two", "three"], [2, 3, 1]);
// Adds a row to the table "two" with id 1. Returns True.
sQL.ins("two", ["first", "second", "third"]);
// Returns the value "third" from the third column
// in the row with id 1 of the table "two".
sQL.sel("two", 1, 3);
// Removes the first row of the table "two".
sQL.rmv("two", 1);
// Returns "<null>" as the cell with id 1
// has been removed from table "two".
sQL.sel("two", 1, 2);
// Returns False as number of columns are not correct.
sQL.ins("two", ["fourth", "fifth"]);
// Adds a row to the table "two" with id 2. Returns True.
sQL.ins("two", ["fourth", "fifth", "sixth"]);
Constraints:
n == names.length == columns.length1 <= n <= 10^41 <= names[i].length, row[i].length, name.length <= 10names[i],row[i], andnameconsist only of lowercase English letters.1 <= columns[i] <= 101 <= row.length <= 10- All
names[i]are distinct. - At most
2000calls will be made toinsandrmv. - At most
10^4calls will be made tosel. - At most
500calls will be made toexp.
Follow-up: Which approach would you choose if the table might become sparse due to many deletions, and why? Consider the impact on memory usage and performance.
Solution
Approach: Hash Table
Store the column count of each table and its live rows in a hash map keyed by row id, plus the next id to hand out. ins checks the table name and row length and then stores the row; rmv deletes by id; sel validates the row and column before reading; exp lists the remaining rows in id order.
Complexity: O(1) average per ins, rmv and sel; exp is linear in the size of the exported table.
class SQL:
def __init__(self, names: List[str], columns: List[int]):
self.cols = dict(zip(names, columns))
self.rows = {name: {} for name in names}
self.nxt = {name: 1 for name in names}
def ins(self, name: str, row: List[str]) -> bool:
if name not in self.cols or len(row) != self.cols[name]:
return False
i = self.nxt[name]
self.rows[name][i] = row
self.nxt[name] = i + 1
return True
def rmv(self, name: str, rowId: int) -> None:
if name in self.rows:
self.rows[name].pop(rowId, None)
def sel(self, name: str, rowId: int, columnId: int) -> str:
row = self.rows.get(name, {}).get(rowId)
if row is None or columnId < 1 or columnId > len(row):
return '<null>'
return row[columnId - 1]
def exp(self, name: str) -> List[str]:
if name not in self.rows:
return []
return [
','.join([str(i), *self.rows[name][i]]) for i in sorted(self.rows[name])
]
# Your SQL object will be instantiated and called as such:
# obj = SQL(names, columns)
# param_1 = obj.ins(name,row)
# obj.rmv(name,rowId)
# param_3 = obj.sel(name,rowId,columnId)
# param_4 = obj.exp(name)