Build the Equation - Problem
You have a very powerful program that can solve any equation of one variable in the world. Given a table Terms containing polynomial terms, you need to build a properly formatted equation.
Table: Terms
| Column Name | Type |
|---|---|
| power | int |
| factor | int |
Requirements:
- The equation format is: LHS = 0
- Each term follows:
<sign><fact>X^<pow> <sign>is either "+" or "-"<fact>is the absolute value of the factor- If power = 1, omit "^1" (e.g., "+3X")
- If power = 0, omit "X" entirely (e.g., "-3")
- Order terms by power in descending order
Table Schema
Terms
| Column Name | Type | Description |
|---|---|---|
power
PK
|
int | The exponent of X in the term (0-100) |
factor
|
int | The coefficient of the term (-100 to 100, non-zero) |
Primary Key: power
Note: Each row represents one term in a polynomial equation. Power values are unique.
Input & Output
Example 1 — Standard Polynomial
Input Table:
| power | factor |
|---|---|
| 2 | 1 |
| 1 | -4 |
| 0 | 2 |
Output:
| equation |
|---|
| +1X^2-4X+2=0 |
💡 Note:
The polynomial has three terms: X² (power=2, factor=1), -4X (power=1, factor=-4), and +2 (power=0, factor=2). Terms are ordered by descending power: 2, 1, 0.
Example 2 — Single Term
Input Table:
| power | factor |
|---|---|
| 0 | 1 |
Output:
| equation |
|---|
| +1=0 |
💡 Note:
A constant term with power=0 results in just the factor value without X. The equation becomes '+1=0'.
Example 3 — Negative Leading Coefficient
Input Table:
| power | factor |
|---|---|
| 4 | -5 |
| 2 | 3 |
| 1 | 1 |
Output:
| equation |
|---|
| -5X^4+3X^2+1X=0 |
💡 Note:
Shows negative leading coefficient (-5X⁴), power=1 formatted as X (not X^1), and proper ordering by descending power.
Constraints
-
poweris an integer in the range[0, 100] -
factoris an integer in the range[-100, 100]and cannot be zero -
powercolumn contains unique values
Visualization
Tap to expand
Understanding the Visualization
1
Input
Terms with power and factor values
2
Format
Apply CASE logic for signs and powers
3
Aggregate
Combine terms into equation string
Key Takeaway
🎯 Key Insight: Use STRING_AGG with CASE logic to handle complex string formatting requirements in SQL
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code