Watch the video solution for Build the Equation, a hard level problem involving Database. This walkthrough by Everyday Data Science has 419 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Terms
+-------------+------+ | Column Name | Type | +-------------+------+ | power | int | | factor | int | +-------------+------+ power is the column with unique values for this table. Each row of this table contains information about one term of the equation. power is an integer in the range [0, 100]. factor is an integer in the range [-100, 100] and cannot be zero.
You have a very powerful program that can solve any equation of one variable in the world. The equation passed to the program must be formatted as follows:
"<sign><fact>X^<pow>" where:
<sign> is either "+" or "-".<fact> is the absolute value of the factor.<pow> is the value of the power.1, do not add "^<pow>".
power = 1 and factor = 3, the term will be "+3X".0, add neither "X" nor "^<pow>".
power = 0 and factor = -3, the term will be "-3".Write a solution to build the equation.
The result format is in the following example.
Example 1:
Input: Terms table: +-------+--------+ | power | factor | +-------+--------+ | 2 | 1 | | 1 | -4 | | 0 | 2 | +-------+--------+ Output: +--------------+ | equation | +--------------+ | +1X^2-4X+2=0 | +--------------+
Example 2:
Input: Terms table: +-------+--------+ | power | factor | +-------+--------+ | 4 | -4 | | 2 | 1 | | 1 | -1 | +-------+--------+ Output: +-----------------+ | equation | +-----------------+ | -4X^4+1X^2-1X=0 | +-----------------+
Follow up: What will be changed in your solution if the power is not a primary key but each power should be unique in the answer?
Problem Overview: Each row represents a polynomial term with a coefficient (factor) and exponent (power). Your job is to convert these rows into a single formatted equation string such as 3x^2+2x-5, ordered by descending power and formatted according to algebra rules.
Approach 1: CASE Formatting + GROUP_CONCAT (O(n log n) time, O(n) space)
The clean SQL solution formats every term independently and then concatenates them into a single string. Use CASE expressions to convert each row into its textual representation: factor only when power = 0, factorx when power = 1, and factorx^power otherwise. Handle positive signs by prefixing '+' when the factor is positive so that intermediate terms concatenate correctly.
Once every row is converted into a formatted term, combine them using MySQL’s GROUP_CONCAT. Apply ORDER BY power DESC inside the aggregation so the highest power appears first, matching the standard polynomial format. Finally, remove a possible leading '+' from the final string with TRIM or SUBSTRING. The database scans all rows and performs a sort during aggregation, giving O(n log n) time and O(n) output space.
This approach relies entirely on SQL string manipulation and aggregation features, which is exactly what interviewers want to see in a database problem. The key insight is treating each polynomial term as a formatted string before aggregation rather than trying to build the equation incrementally.
Approach 2: Incremental Concatenation with Variables (O(n log n) time, O(n) space)
Another option uses MySQL user-defined variables to iteratively build the equation string. First sort rows by power DESC. Then iterate through the ordered rows while appending formatted terms to a variable using CONCAT. Each row still uses CASE logic to format the correct representation (x, x^power, or constant).
This method mimics procedural string building inside SQL. While it works, it is more verbose and less portable across SQL engines. Modern SQL solutions generally prefer aggregation functions like GROUP_CONCAT because they express the transformation declaratively and integrate naturally with SQL query patterns.
Recommended for interviews: The CASE + GROUP_CONCAT approach is the expected solution. It shows you understand SQL aggregation, conditional formatting, and ordering inside aggregation. A procedural variable approach demonstrates the same idea but is less idiomatic. Interviewers usually want to see strong string manipulation combined with SQL aggregation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| CASE + GROUP_CONCAT aggregation | O(n log n) | O(n) | Best general SQL solution when building a formatted string from multiple rows |
| Procedural concatenation with variables | O(n log n) | O(n) | Useful when aggregation functions are unavailable or procedural SQL logic is preferred |