मध्यम 0 हल किए गए 0% स्वीकृति

SQL Query Builder

Assemble a SQL query from parts given in any order, the way a builder object collects settings step by step.

आप क्या सीखेंगे

  • Collect parts in any order before building
  • Apply defaults for missing parts
  • Assemble a result in a fixed format

समस्या

अनुवाद दिखाएँ

The builder pattern constructs a complex object step by step and produces the final result at the end, so callers can set the parts in any order.

Each part is a keyword, a space, and its text:

  • select <columns>: comma-separated column names (spaces around commas may vary). Optional; the default is *.
  • from <table>: required.
  • where <condition>, order <columns>, limit <n>: optional.

If a keyword appears more than once, the last one wins. Build the query in this fixed order, with single spaces:

SELECT <columns> FROM <table>[ WHERE <condition>][ ORDER BY <columns>][ LIMIT <n>]

In the SELECT list, trim each column and join them with , . All other texts are copied unchanged.

Implement query_builder(parts).

Input

parts: The parts, in any order.

Output

Return the SQL query string.

सीमाएँ

1 ≤ number of parts ≤ 20

At least one from part; each part has at most 100 characters

उदाहरण

उदाहरण 1
इनपुट:
parts = ["where age \u003e 18", "from users", "select name,age"]

अपेक्षित आउटपुट:
expected = "SELECT name, age FROM users WHERE age \u003e 18"
इनपुट:
std::vector<std::string> parts = {"where age \u003e 18", "from users", "select name,age"};

अपेक्षित आउटपुट:
std::string expected = "SELECT name, age FROM users WHERE age \u003e 18";
इनपुट:
string[] parts = new string[] {"where age \u003e 18", "from users", "select name,age"};

अपेक्षित आउटपुट:
string expected = "SELECT name, age FROM users WHERE age \u003e 18";

व्याख्या

The parts arrive out of order, but the query is always built as SELECT … FROM … WHERE …: "SELECT name, age FROM users WHERE age > 18".

उदाहरण 2
इनपुट:
parts = ["from orders", "limit 10", "order total DESC"]

अपेक्षित आउटपुट:
expected = "SELECT * FROM orders ORDER BY total DESC LIMIT 10"
इनपुट:
std::vector<std::string> parts = {"from orders", "limit 10", "order total DESC"};

अपेक्षित आउटपुट:
std::string expected = "SELECT * FROM orders ORDER BY total DESC LIMIT 10";
इनपुट:
string[] parts = new string[] {"from orders", "limit 10", "order total DESC"};

अपेक्षित आउटपुट:
string expected = "SELECT * FROM orders ORDER BY total DESC LIMIT 10";

व्याख्या

Without a select part the columns default to *: "SELECT * FROM orders ORDER BY total DESC LIMIT 10".

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Store each part by its keyword first; build the text only at the end.

संकेत 2

A later part with the same keyword replaces the earlier one.

संकेत 3

Trim each column in the SELECT list and use * when there is no select part.

अपेक्षित जटिलता

O(total length) time, O(total length) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।