Design SQL — LeetCode 2408 Python Solution

MediumLeetCode PremiumDesignArrayHash TableString
Problem
#2408
Pattern
Hash Map
Reading time
3 min

The problem

You are given two string arrays, names and columns, both of size n. The ith table is represented by the name names[i] and contains columns[i] number of columns.

Python solution

Python
class SQL:
    def __init__(self, names: List[str], columns: List[int]):
        self.tables = defaultdict(list)

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

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

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


# Your SQL object will be instantiated and called as such:
# obj = SQL(names, columns)
# obj.insertRow(name,row)
# obj.deleteRow(name,rowId)
# param_3 = obj.selectCell(name,rowId,columnId)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2408. Design SQL is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2408. Design SQL?
LeetCode 2408. Design SQL is rated Medium on LeetCode.
What is the time complexity of LeetCode 2408. Design SQL?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2408. Design SQL?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2408. Design SQL cover?
LeetCode 2408. Design SQL is tagged Design, Array, Hash Table and String on LeetCode.
Is LeetCode 2408. Design SQL a premium problem?
Yes. LeetCode 2408. Design SQL is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview