Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Table: Products
+-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | store1 | int | | store2 | int | | store3 | int | +-------------+---------+ product_id is the primary key (column with unique values) for this table. Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3. If the product is not available in a store, the price will be null in that store's column.
Write a solution to rearrange the Products table so that each row has (product_id, store, price). If a product is not available in a store, do not include a row with that product_id and store combination in the result table.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Products table: +------------+--------+--------+--------+ | product_id | store1 | store2 | store3 | +------------+--------+--------+--------+ | 0 | 95 | 100 | 105 | | 1 | 70 | null | 80 | +------------+--------+--------+--------+ Output: +------------+--------+-------+ | product_id | store | price | +------------+--------+-------+ | 0 | store1 | 95 | | 0 | store2 | 100 | | 0 | store3 | 105 | | 1 | store1 | 70 | | 1 | store3 | 80 | +------------+--------+-------+ Explanation: Product 0 is available in all three stores with prices 95, 100, and 105 respectively. Product 1 is available in store1 with price 70 and store3 with price 80. The product is not available in store2.
Problem summary: Table: Products +-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | store1 | int | | store2 | int | | store3 | int | +-------------+---------+ product_id is the primary key (column with unique values) for this table. Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3. If the product is not available in a store, the price will be null in that store's column. Write a solution to rearrange the Products table so that each row has (product_id, store, price). If a product is not available in a store, do not include a row with that product_id and store combination in the result table. Return the result table in any order. The result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Products":["product_id","store1","store2","store3"]},"rows":{"Products":[[0, 95, 100, 105], [1, 70, null, 80]]}}products-price-for-each-store)dynamic-unpivoting-of-a-table)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1795: Rearrange Products Table
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1795: Rearrange Products Table
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1795: Rearrange Products Table
// # Write your MySQL query statement below
// SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
// UNION
// SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
// UNION
// SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
// "#
// }
// Accepted solution for LeetCode #1795: Rearrange Products Table
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1795: Rearrange Products Table
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1795: Rearrange Products Table
// # Write your MySQL query statement below
// SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
// UNION
// SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
// UNION
// SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
// "#
// }
# Accepted solution for LeetCode #1795: Rearrange Products Table
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1795: Rearrange Products Table
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1795: Rearrange Products Table
# # Write your MySQL query statement below
# SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
# UNION
# SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
# UNION
# SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
# "#
# }
// Accepted solution for LeetCode #1795: Rearrange Products Table
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1795: Rearrange Products Table
# Write your MySQL query statement below
SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
UNION
SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
UNION
SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
"#
}
// Accepted solution for LeetCode #1795: Rearrange Products Table
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1795: Rearrange Products Table
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1795: Rearrange Products Table
// # Write your MySQL query statement below
// SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
// UNION
// SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
// UNION
// SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.