FastPrepIn-Memory Database Queries
Problems/In-Memory Database Queries

In-Memory Database Queries

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reports from April 2024 to September 2026 describe a coding round where you build a small in-memory database. They mention adding rows, reading them back, filters such as greater-than, sorting with ORDER BY, and deleting rows with tombstones. Two of them say the round came in several parts. One mentions passing tests.

None of them gives the exact commands, value types, output format or limits. Here the commands come as a list of tokens. Columns are int or str. A WHERE clause joins conditions with AND. Equal rows keep their insertion order. A query returns its rows as one string. A delete returns how many rows it removed. Tombstones and the part-by-part build are covered in the editorial and are not tested. The examples are our own.

Our version is about 70% the same as the reported questions.

Build a small in-memory database. It keeps tables of rows and answers simple SQL-like commands. You get the commands as a list operations. Run them in order and return the outputs.

Each operation is a list of string tokens. There are four kinds:

  • ["CREATE", table, column1, ..., columnK] creates an empty table. Each column token is a name and a type joined by a colon, such as "age:int" or "name:str". It has no output.
  • ["INSERT", table, value1, ..., valueK] adds one row, with one value for each column in column order. It has no output.
  • ["SELECT", table, columns], optionally followed by a WHERE clause and then an ORDER BY clause, returns rows. columns is "*" for every column in table order, or column names joined by commas, such as "name,age".
  • ["DELETE", table], optionally followed by a WHERE clause, removes the matching rows. Without a WHERE clause it removes every row.

Filtering with WHERE

A WHERE clause is the token "WHERE" and one condition, then zero or more "AND" tokens each followed by another condition. A condition is three tokens: a column, an operator and a value, such as "age", ">", "25". The operators are =, !=, <, <=, > and >=. A row matches when every condition holds.

Values compare by the column's type. int values compare as numbers, so 9 is less than 10. str values compare as text, one character at a time by character code, so digits come before letters, "b10" is less than "b9", and a string comes before any longer string that starts with it.

Sorting with ORDER BY

An ORDER BY clause is the tokens "ORDER" and "BY", then one or more pairs of a column and a direction, "ASC" (small to large) or "DESC" (large to small). Rows are sorted by the first pair. A later pair only decides between rows that are equal on every earlier column. Rows that are equal on every sort column, and all rows when there is no ORDER BY, keep the order in which they were inserted.

Outputs

  • A SELECT outputs one string. Each matching row becomes its chosen column values joined by ",", and the rows are joined by ";". Values are written exactly as they were inserted. If no row matches, the output is the empty string "".
  • A DELETE outputs the number of rows it removed, as a decimal string. A row that was already removed is not counted again.

Return the outputs of all SELECT and DELETE operations in order.

Function

runDatabase(operations: String[][]) → String[]

Examples

Example 1

operations = [["CREATE","users","id:int","name:str","age:int"],["INSERT","users","1","ana","31"],["INSERT","users","2","bo","25"],["INSERT","users","3","cy","40"],["INSERT","users","4","di","31"],["SELECT","users","name,age","WHERE","age",">","25","ORDER","BY","age","DESC"],["DELETE","users","WHERE","age","<=","25"],["SELECT","users","*"]]return = ["cy,40;ana,31;di,31","1","1,ana,31;3,cy,40;4,di,31"]

The first query keeps rows with age above 25: ana (31), cy (40) and di (31). Sorting by age from high to low puts cy first. ana and di tie at 31, so they keep the order they were inserted in.

The delete removes bo, the only row with age at most 25, so it outputs "1". The last query lists every column of the three rows that are left, in insertion order.

Example 2

operations = [["CREATE","items","code:str","qty:int"],["INSERT","items","b10","9"],["INSERT","items","b9","10"],["INSERT","items","a","100"],["INSERT","items","b9","7"],["SELECT","items","code,qty","ORDER","BY","qty","ASC"],["SELECT","items","code,qty","ORDER","BY","code","ASC"],["SELECT","items","qty","WHERE","code",">=","b","AND","qty","<","10"]]return = ["b9,7;b10,9;b9,10;a,100","a,100;b10,9;b9,10;b9,7","9;7"]

qty is an int column, so it sorts as numbers: 7, 9, 10, 100.

code is a str column, so it sorts as text, one character at a time. "b10" comes before "b9" because '1' comes before '9'. The two "b9" rows tie, so the one inserted first (qty 10) stays first.

The last query needs both conditions. "b10" and the second "b9" are at least "b" and have qty below 10, so the output is "9;7". The row with code "a" fails because "a" comes before "b".

Example 3

operations = [["CREATE","logs","day:int","level:str"],["CREATE","t","x:int"],["INSERT","logs","2","warn"],["INSERT","logs","1","info"],["INSERT","logs","2","error"],["INSERT","t","5"],["DELETE","logs","WHERE","level","=","info"],["DELETE","logs","WHERE","level","=","info"],["SELECT","logs","*","WHERE","day","!=","2"],["SELECT","logs","*","ORDER","BY","day","ASC","level","ASC"],["DELETE","t"],["SELECT","t","*"]]return = ["1","0","","2,error;2,warn","1",""]

The first delete removes the one info row and outputs "1". The second delete finds nothing left to remove and outputs "0".

No remaining row has day other than 2, so that query outputs the empty string. The next query sorts by day, and the tie at day 2 is broken by level: "error" comes before "warn".

Tables are separate. Deleting every row of t outputs "1", and then t is empty.

Constraints

  • 1 <= operations.length <= 2000
  • 2 <= operations[i].length <= 27
  • Every token has 1 to 65 characters.
  • There are at most 5 tables, and each table has 1 to 6 columns.
  • Table and column names have 1 to 10 lowercase English letters. Column names in one table are distinct, and each column type is int or str.
  • An int value is an integer from -10^9 to 10^9, written in base 10 with no plus sign and no leading zeros.
  • A str value has 1 to 10 characters, each a lowercase English letter or a digit.
  • Every operation is well formed: CREATE uses a new table name, every other operation names an existing table and its columns, INSERT gives exactly one value per column, and every value fits its column's type.
  • A WHERE clause has 1 to 4 conditions. An ORDER BY clause has 1 to 3 distinct columns. A column list is "*" or 1 to 6 distinct column names.

More OpenAI Onsite Interview questions

  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Message Event AggregationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • 15-Minute Chat Session TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Segmented Durable Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Durable Work Queue OperationsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Versioned Social NetworkPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Budgeted API Inference with Out-of-Order FeedbackONSITE INTERVIEW · Seen Oct 2026
  • Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Count Friend CirclesPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Order Manager with Partial Cancellations and Indexed QueriesStripe · ONSITE INTERVIEWStripe interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
  • Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
  • Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
  • Validate Server Log Summary CountsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
  • Banking System with Top ActivityCapital One · ONSITE INTERVIEWCapital One interview questions
  • Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public String[] runDatabase(String[][] operations) {
        // Write your code here.
    }
}
Checking account…