KatInterview

Design a Fluent Query Builder with Chaining and Join Support

Data Structure DesignModel answer
MediumModel answerData Structure Designapi-designfluent-interfacesql-generation

Last updated

Build a Python query builder that chains table, select, where, alias, and join methods, rendering to SQL strings that match an exact expected format.

Design and implement a query builder in Python that supports a fluent, chainable API. Your implementation will be tested against an existing suite that calls methods and compares the output of render() against expected SQL strings.

The entry point provides two factory methods:

  • o.table(name) — starts a query for the given table
  • o.cond(column, operator, value) — creates a condition object

The query object returned by table() supports these methods:

  • select(*columns) — specifies which columns to retrieve
  • where(*conditions) — filters rows using condition objects
  • alias(name) — assigns a table alias
  • join(other_query, using=column) — joins with another query on a shared column
  • render() — returns the final SQL string

Example Usage

# Simple query
o.table("users").select("id", "email").render()

# Query with conditions
o.table("products").select("id", "upc", "volume").where(
    o.cond("volume", ">", 300),
    o.cond("upc", "=", "1337")
).render()

# Query with alias
o.table("products").select("product_id", "price").alias("p").render()

# Joined queries
users = o.table("users").select("user_id", "email").alias("u")
apps = o.table("applications").select("loan_id").alias("app")
users.join(apps, using="user_id").render()

Requirements

  • Each method call returns a query object, enabling method chaining
  • The render() method produces a SQL SELECT statement matching the exact format expected by the tests
  • Conditions created with cond support at least = and > operators
  • Multiple conditions passed to where are combined appropriately
  • When queries are joined, the shared column specified in using determines the join relationship
  • The exact formatting details (keyword case, spacing, use of AS, semicolons, etc.) are determined by the test expectations

Questions to Clarify

Before implementing, confirm with the interviewer:

  1. What exact SQL format do the tests expect? (Keyword case, spacing, whether AS appears before aliases, semicolon at end)
  2. When a table has an alias, should selected columns be qualified with it (e.g., p.price)?
  3. How are multiple conditions combined? With AND? Can where be called multiple times?
  4. Which operators must be supported beyond = and >?
  5. How should string values with embedded quotes be escaped?
  6. Should methods return new query objects (immutable) or modify the existing one (mutable)?
  7. In a joined query, which columns appear in the final SELECT clause and in what order?
  8. How should None values render in conditions?

Hints

Hint 1

Store the query components (table, columns, conditions, alias, joins) as internal state and assemble them only when render() is called

Hint 2

Decide early whether your builder is mutable or immutable—the join example suggests mutation since the result isn't reassigned

Hint 3

When qualifying column names in joins, check whether a table has an alias and use it; otherwise use the table name