← 返回 twosigma 的题目列表Implement an In-Memory Database with Simple SQL-like Commands
类型:online_judge
Problem: Implement a Simple In-Memory Database (In-Memory DB)
Implement an in-memory database engine that supports three types of commands: create table, insert into, and select ... where ....
Input Format
Input is queries: List[List[str]].
Each queries[i] is a tokenized command (already split into an array of strings).
Execute all commands sequentially.
Supported Commands
1) Create Table
Create a table:
create table tablename ( col1 col2 col3 ... )
tablename is the table name.
The parenthesized part is a list of column names.
2) Insert Into
Insert one row:
insert into tablename ( v1 v2 v3 ... )
v1..vk are integers.
The number of values equals the number of columns in the table.
3) Select With WHERE
Query with conditions:
select from tablename where ( fieldA op1 valueA AND fieldB op2 valueB AND ... )
The where clause only needs to support conditions connected by AND (no OR, no nested parentheses).
Each condition is: field op value
field: column name
value: integer
op: one of =, !=, >, >=, <, <=
Data Constraints
All column values are int.
Output Requirements
For each select command, output all rows that satisfy the conditions.
Each row is an integer array in the original column order.
If no rows match, output an empty array.
Return a list of outputs for all select commands in order.
Robustness / Assumptions
The query syntax may not be fully valid; you may implement minimal validation to keep the program running (e.g., ignore invalid commands/fields, or handle them in a defined way).
Sample Tests (5 cases)
Case 1
Input:
[
["create","table","t","(","a","b","")"],
["insert","into","t","(","1","2",")"],
["insert","into","t","(","3","4",")"],
["select","from","t","where","(","a",">=","2","AND","b","<=","4",")"]
]
Output:
[[[3,4]]]
Case 2
Input:
[
["create","table","users","(","id","age",")"],
["insert","into","users","(","1","20",")"],
["insert","into","users","(","2","30",")"],
["select","from","users","where","(","age",">","25",")"]
]
Output:
[[[2,30]]]
Case 3
Input:
[
["create","table","x","(","c1","c2","c3",")"],
["insert","into","x","(","1","1","1",")"],
["insert","into","x","(","1","2","3",")"],
["select","from","x","where","(","c1","=","1","AND","c2","=","1",")"]
]
Output:
[[[1,1,1]]]
Case 4
Input:
[
["create","table","t","(","a","b",")"],
["insert","into","t","(","5","6",")"],
["select","from","t","where","(","a","!=","5",")"]
]
Output:
[[[]]]
Case 5
Input:
[
["create","table","t","(","a","b",")"],
["insert","into","t","(","10","20",")"],
["insert","into","t","(","10","30",")"],
["select","from","t","where","(","a","=","10","AND","b",">=","25",")"]
]
Output:
[[[10,30]]]
Example
Input
create table t ( a b )
insert into t ( 1 2 )
insert into t ( 3 4 )
select from t where ( a >= 2 AND b <= 4 )
Output
[[[3, 4]]]