In-Memory Table Queries

Asked at OpenAIPhone screen

Reported by the problem's creator, not verified

Build an in-memory table MemoryDB that stores records and answers queries over them. A record is a dictionary with string field names and values that are integers or strings. Different records may have different fields; there is no declared schema.

A condition is a list [field, op, value] where op is one of "=", "!=", "<", ">", "<=", ">=". A row satisfies a condition only if it has field, its value for field has the same type as value (both integers or both strings), and the comparison holds. Strings compare in dictionary order. A row that lacks the field, or holds a value of the other type, fails the condition for every operator, including "!=". A list of conditions is joined by AND; an empty list is satisfied by every row.

Each test drives one MemoryDB() instance through a sequence of calls and checks every return value.

Examples

  1. Example 1

    Input: init = [], operations = [["insert",[{"id":1,"age":30,"name":"Alice"}]],["insert",[{"id":2,"age":25,"name":"Bob"}]],["select",[[],[["age",">",25]]]],["select",[["name"],[["age",">=",25]]]],["select",[[],[]]]]

    Output: [null,null,[{"id":1,"age":30,"name":"Alice"}],[{"name":"Alice"},{"name":"Bob"}],[{"id":1,"age":30,"name":"Alice"},{"id":2,"age":25,"name":"Bob"}]]

    Explanation: Alice is 30 and Bob is 25, so only Alice has age > 25. The second query keeps both rows (age >= 25) but returns only the name column. An empty condition list matches every row and returns full records.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.