Implement a small in-memory table engine that begins with a CSV string and then executes a sequence of SQL-like commands.
The first row of the CSV contains column names. Remaining rows contain data. Fields are comma-separated; quoted fields may contain commas and escaped double quotes.
Support the following commands:
SELECT col1,col2,... WHERE col=value: return matching rows with the selected columns joined by commasINSERT value1,value2,...: append one rowDELETE WHERE col=value: delete every matching row
Process commands in order and return every output row produced by the SELECT commands.
Complete the function runInMemorySql in the editor below.
runInMemorySql has the following parameters:
String csv: the initial table contentsString[] commands: the commands to execute in order
Returns
String[]: the output rows produced by all SELECT commands, in order.
Examples
01 · Example 1
csv = "name,age,city\nAlice,30,NY\nBob,25,SF" commands = ["SELECT name WHERE city=NY", "INSERT Charlie,22,NY", "SELECT name,age WHERE city=NY"] return = ["Alice", "Alice,30", "Charlie,22"]
The first query matches Alice. After inserting Charlie, the second query returns both NY rows in table order.
02 · Example 2
csv = "name,age\nAlice,30\nBob,25" commands = ["DELETE WHERE name=Alice", "SELECT name,age WHERE age=25"] return = ["Bob,25"]
Deleting Alice leaves only Bob, who matches the final query.
Constraints
- The total number of rows plus commands can be as large as
10^5. - Quoted CSV fields may contain commas and doubled quotes.
- Only equality filters are required for this problem version.
More Microsoft problems
- Rank Open BusinessesPHONE SCREEN · Seen May 2026
- Retain Top K ValuesPHONE SCREEN · Seen May 2026
- Order Records by Matching Start and EndONSITE INTERVIEW · Seen May 2026
- Recover Corrupted Master PageONSITE INTERVIEW · Seen Feb 2026
- Distinct Number Line MovesOA · Seen Oct 2025
- Minimum Round Trip LengthsOA · Seen Aug 2025
- Programmer StringsOA · Seen Aug 2025
- Get Minimum TimeSeen Jun 2025
public String[] runInMemorySql(String csv, String[] commands) {
// write your code here
}
csv"name,age,city\nAlice,30,NY\nBob,25,SF"
commands["SELECT name WHERE city=NY", "INSERT Charlie,22,NY", "SELECT name,age WHERE city=NY"]
expected["Alice", "Alice", "30", "Charlie", "22"]
sign in to submit