Design SQL - 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.
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 n tables.bool ins(String name, String[] row)Inserts row into the table name and returns true. If row.length does not match the expected number of columns, or name is not a valid table, returns false without any insertion.void rmv(String name, int rowId)Removes the row rowId from the table name. If name is not a valid table or there is no row with id rowId, no removal is performed.String sel(String name, int rowId, int columnId)Returns the value of the cell at the specified rowId and columnId in the table name. If name is not a valid table, or the cell (rowId, columnId) is invalid, returns "<null>".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 ",".
Input & Output
Example 1 — Basic Operations
$
Input:
names = ["users", "products"], columns = [3, 2]
›
Output:
SQL object created with two tables
💡 Note:
Creates a SQL database with 'users' table (3 columns) and 'products' table (2 columns). Auto-increment IDs start at 1 for each table.
Example 2 — Insert and Select
$
Input:
ins("users", ["John", "25", "Engineer"]) → sel("users", 1, 2)
›
Output:
true → "25"
💡 Note:
Insert row into users table gets ID 1. Selecting row 1, column 2 returns "25" (the age field).
Example 3 — Export Table
$
Input:
After inserting two rows → exp("users")
›
Output:
["1,John,25,Engineer", "2,Jane,30,Manager"]
💡 Note:
Export returns all rows in CSV format with row ID as first column, followed by data columns.
Constraints
- 1 ≤ n ≤ 100
- 1 ≤ names[i].length ≤ 20
- 1 ≤ columns[i] ≤ 20
- All table names are unique
- At most 1000 calls to ins, rmv, sel, and exp in total
Visualization
Tap to expand
Understanding the Visualization
1
Input
Table names and column counts define database schema
2
Process
Hash maps provide O(1) access to tables and rows
3
Output
Efficient database operations: insert, remove, select, export
Key Takeaway
🎯 Key Insight: Hash maps transform O(n) linear searches into O(1) constant-time lookups for all database operations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code