Design SQL - Problem
Design a Simplified SQL Database System
You're tasked with building a lightweight in-memory database that mimics core SQL operations! Given two string arrays
Your SQL class must support:
๐น INSERT: Add rows with auto-incrementing IDs (starting from 1)
๐น REMOVE: Delete specific rows by ID
๐น SELECT: Retrieve individual cell values
๐น EXPORT: Generate CSV format of entire tables
Key Challenge: Auto-increment IDs never reset, even after deletions! Each table maintains its own ID sequence.
Example: If you insert 3 rows (IDs: 1,2,3), delete row 2, then insert again, the new row gets ID 4, not 2.
You're tasked with building a lightweight in-memory database that mimics core SQL operations! Given two string arrays
names and columns of size n, where the i-th table has name names[i] and columns[i] number of columns.Your SQL class must support:
๐น INSERT: Add rows with auto-incrementing IDs (starting from 1)
๐น REMOVE: Delete specific rows by ID
๐น SELECT: Retrieve individual cell values
๐น EXPORT: Generate CSV format of entire tables
Key Challenge: Auto-increment IDs never reset, even after deletions! Each table maintains its own ID sequence.
Example: If you insert 3 rows (IDs: 1,2,3), delete row 2, then insert again, the new row gets ID 4, not 2.
Input & Output
basic_operations.py โ Python
$
Input:
names = ["users", "orders"], columns = [3, 2]
Operations:
- ins("users", ["john", "25", "engineer"])
- ins("orders", ["laptop", "999"])
- sel("users", 1, 2)
- exp("users")
โบ
Output:
[true, true, "25", ["1,john,25,engineer"]]
๐ก Note:
Creates 2 tables, inserts rows with auto-increment IDs (both get ID=1), selects column 2 from users row 1 (returns "25"), then exports users table in CSV format.
auto_increment_after_deletion.py โ Python
$
Input:
names = ["products"], columns = [2]
Operations:
- ins("products", ["phone", "800"])
- ins("products", ["tablet", "600"])
- rmv("products", 1)
- ins("products", ["laptop", "1200"])
- exp("products")
โบ
Output:
[true, true, null, true, ["2,tablet,600", "3,laptop,1200"]]
๐ก Note:
Inserts 2 rows (IDs 1,2), removes row 1, then inserts new row. The new row gets ID=3 (not 1), showing auto-increment never resets. Export shows remaining rows 2 and 3.
edge_cases.py โ Python
$
Input:
names = ["test"], columns = [1]
Operations:
- ins("invalid_table", ["data"])
- ins("test", ["a", "b"])
- sel("test", 999, 1)
- sel("test", 1, 999)
- exp("invalid_table")
โบ
Output:
[false, false, "<null>", "<null>", []]
๐ก Note:
Tests edge cases: invalid table name returns false, wrong column count returns false, invalid row/column IDs return "", and invalid table export returns empty array.
Visualization
Tap to expand
Understanding the Visualization
1
Database Creation
Initialize main hash table with table names as keys, each pointing to table metadata
2
Table Structure
Each table contains column count, row hash map, and auto-increment counter
3
Row Operations
Perform CRUD operations with O(1) complexity using direct hash access
4
Export Generation
Traverse row hash map and generate CSV with sorted row IDs
Key Takeaway
๐ฏ Key Insight: Use hash maps at both table and row levels to achieve O(1) performance for all operations, while maintaining separate auto-increment counters per table for proper ID management.
Time & Space Complexity
Time Complexity
O(1)
Hash map provides O(1) average access for both table lookup and row operations
โ Linear Growth
Space Complexity
O(n*m*k)
Same space as brute force but with hash map overhead, where n=tables, m=rows, k=avg columns
โก Linearithmic Space
Constraints
- 1 โค names.length = columns.length โค 100
- 1 โค names[i].length โค 20
- 1 โค columns[i] โค 20
- At most 104 operations will be called
- 1 โค row.length โค 20 for insert operations
- 1 โค rowId, columnId โค 104 for access operations
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code