As reported: Yahoo coding challenge (2025, round type not stated): write a query API for a mail storage system — a method that takes a query structure and returns a SQL string (not executed). Standard operators (AND, OR, =, !=, <=, >=) and a full-text
SEARCH(column, text)function on aMessagetable; use cases: filter a folder ordered by delivery time, paginate, OR several conditions, AND conditions, and nest them, e.g.FolderId=1 OR (FolderId=2 AND SEARCH(Subject,"test")). Signature:query(columns, whereClause, orders, limit, offset).
Mail is stored in one table:
Message(UserId Int, MessageId Int, Subject String, DeliveryTs Int, From String,
To String, Body String, FolderId Int, ConversationId String)
Implement query(columns, where, orders, limit, offset), which returns a SQL string (it is not executed):
columns— the columns to select; an empty list or["*"]means*.where— aWhereClauseor none. AWhereClausehas a logicaloperator(AND/OR), a list offilters(FilterCriteria) and a list of nestedclauses(WhereClause), which may nest to any depth.FilterCriteria(column, operator, value)—operatorisEQUALS,NOT_EQUALS,LESS_EQUAL,GREATER_EQUALorSEARCH;valueis given as a string.orders— a list ofSortOrder(column, direction)with directionASC/DESC.limit,offset— integers or none.
Output format (exactly):
SELECT <columns> FROM Message[ WHERE <condition>][ ORDER BY <col> <dir>, ...][ LIMIT <n>][ OFFSET <m>]
- Columns are joined with
", ". - A filter renders without spaces:
col=v,col!=v,col<=v,col>=v, orSEARCH(col,"text"). - Values of Int columns are written as given (
FolderId=1). Values of String columns, and everySEARCHtext, are written in double quotes; inside them,\becomes\\and"becomes\". - A clause's terms are its filters (in order) followed by its nested clauses (in order), joined with
" AND "or" OR ". A nested clause with two or more terms is wrapped in parentheses; one with a single term is not; an empty one is dropped. The top-level condition is never wrapped. WHEREis omitted when the condition is empty;LIMIT/OFFSETare omitted when none.
Example 1
columns = ["*"], where = AND[ FolderId EQUALS "1" ], orders = [DeliveryTs DESC], limit = none, offset = none
Output: SELECT * FROM Message WHERE FolderId=1 ORDER BY DeliveryTs DESC
Example 2
columns = ["*"], where = AND[ FolderId EQUALS "1" ], orders = [Subject ASC], limit = 100, offset = 100
Output: SELECT * FROM Message WHERE FolderId=1 ORDER BY Subject ASC LIMIT 100 OFFSET 100
Example 3
columns = ["MessageId","Subject"]
where = OR[ Subject SEARCH "Promotion", From EQUALS "[email protected]", Body SEARCH "Order Number" ]
Output: SELECT MessageId, Subject FROM Message WHERE SEARCH(Subject,"Promotion") OR From="[email protected]" OR SEARCH(Body,"Order Number")
Example 4
columns = ["*"], where = OR[ FolderId EQUALS "1", AND[ FolderId EQUALS "2", Subject SEARCH "test" ] ], orders = [DeliveryTs DESC]
Output: SELECT * FROM Message WHERE FolderId=1 OR (FolderId=2 AND SEARCH(Subject,"test")) ORDER BY DeliveryTs DESC
Constraints: at most 200 filters in total; nesting depth at most 20; column names are from the table; Int values are decimal integers.
Code runner format: five JSON lines: columns; where as nested arrays [operator, [[column, op, value], ...], [nested clauses...]] or null; orders as [[column, direction], ...]; limit; offset (null for none). The runner builds the objects. In C++ and Go, a missing where is a null pointer and a missing limit/offset is -1.