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
Design SQL - Hash Map Solution INPUT names array: "users" "products" columns array: 3 2 Table Structure: users col1 col2 col3 id=auto products col1 col2 id=auto Operations: insert(name, row) remove(name, id) select(name, id, col) export(name) ALGORITHM STEPS 1 Initialize Tables HashMap: name --> table Store column count per table 2 Insert Operation Auto-increment ID counter Store row in HashMap by ID 3 Remove Operation Delete row from HashMap ID counter unchanged 4 Select/Export Access by table + row ID Export iterates all rows tables = HashMap { "users": {rows, nextId} "products": {rows, nextId} } rows = HashMap{id: [values]} FINAL RESULT SQL Object Created: users (3 columns) nextId: 1 rows: HashMap (empty) columns: 3 Status: OK - Ready products (2 columns) nextId: 1 rows: HashMap (empty) columns: 2 Status: OK - Ready Output: SQL object with 2 tables initialized All operations O(1) average! Key Insight: Use nested HashMaps for O(1) operations: outer map stores tables by name, inner map stores rows by auto-increment ID. The ID counter persists even after deletions, ensuring unique IDs. Each table tracks its own nextId independently. TutorialsPoint - Design SQL | Hash Map Based Design
Asked in
Amazon 35 Google 28 Microsoft 22 Facebook 18
23.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen