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 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
SQL Database ArchitectureMain Hash TableTable Names โ†’ TablesusersordersproductsTable Metadatacolumns: 3, nextId: 4rows: HashMapRow Hash Map1 โ†’ Row2 โ†’ Row3 โ†’ Row5 โ†’ RowPerformance Benefitsโœ“ O(1) table lookupโœ“ O(1) row accessโœ“ O(1) insertionsAuto-increment IDs never reset!
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

n
2n
โœ“ 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

n
2n
โšก 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
Asked in
Google 28 Amazon 35 Meta 22 Microsoft 31 Apple 18 Netflix 15
89.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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