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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Products
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | new_price | int | | change_date | date | +---------------+---------+ (product_id, change_date) is the primary key (combination of columns with unique values) of this table. Each row of this table indicates that the price of some product was changed to a new price at some date.
Initially, all products have price 10.
Write a solution to find the prices of all products on the date 2019-08-16.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Products table: +------------+-----------+-------------+ | product_id | new_price | change_date | +------------+-----------+-------------+ | 1 | 20 | 2019-08-14 | | 2 | 50 | 2019-08-14 | | 1 | 30 | 2019-08-15 | | 1 | 35 | 2019-08-16 | | 2 | 65 | 2019-08-17 | | 3 | 20 | 2019-08-18 | +------------+-----------+-------------+ Output: +------------+-------+ | product_id | price | +------------+-------+ | 2 | 50 | | 1 | 35 | | 3 | 10 | +------------+-------+
Problem summary: Table: Products +---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | new_price | int | | change_date | date | +---------------+---------+ (product_id, change_date) is the primary key (combination of columns with unique values) of this table. Each row of this table indicates that the price of some product was changed to a new price at some date. Initially, all products have price 10. Write a solution to find the prices of all products on the date 2019-08-16. 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","new_price","change_date"]},"rows":{"Products":[[1,20,"2019-08-14"],[2,50,"2019-08-14"],[1,30,"2019-08-15"],[1,35,"2019-08-16"],[2,65,"2019-08-17"],[3,20,"2019-08-18"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1164: Product Price at a Given Date
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1164: Product Price at a Given Date
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1164: Product Price at a Given Date
// # Write your MySQL query statement below
// WITH
// T AS (SELECT DISTINCT product_id FROM Products),
// P AS (
// SELECT product_id, new_price AS price
// FROM Products
// WHERE
// (product_id, change_date) IN (
// SELECT product_id, MAX(change_date) AS change_date
// FROM Products
// WHERE change_date <= '2019-08-16'
// GROUP BY 1
// )
// )
// SELECT product_id, IFNULL(price, 10) AS price
// FROM
// T
// LEFT JOIN P USING (product_id);
// "#
// }
// Accepted solution for LeetCode #1164: Product Price at a Given Date
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1164: Product Price at a Given Date
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1164: Product Price at a Given Date
// # Write your MySQL query statement below
// WITH
// T AS (SELECT DISTINCT product_id FROM Products),
// P AS (
// SELECT product_id, new_price AS price
// FROM Products
// WHERE
// (product_id, change_date) IN (
// SELECT product_id, MAX(change_date) AS change_date
// FROM Products
// WHERE change_date <= '2019-08-16'
// GROUP BY 1
// )
// )
// SELECT product_id, IFNULL(price, 10) AS price
// FROM
// T
// LEFT JOIN P USING (product_id);
// "#
// }
# Accepted solution for LeetCode #1164: Product Price at a Given Date
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1164: Product Price at a Given Date
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1164: Product Price at a Given Date
# # Write your MySQL query statement below
# WITH
# T AS (SELECT DISTINCT product_id FROM Products),
# P AS (
# SELECT product_id, new_price AS price
# FROM Products
# WHERE
# (product_id, change_date) IN (
# SELECT product_id, MAX(change_date) AS change_date
# FROM Products
# WHERE change_date <= '2019-08-16'
# GROUP BY 1
# )
# )
# SELECT product_id, IFNULL(price, 10) AS price
# FROM
# T
# LEFT JOIN P USING (product_id);
# "#
# }
// Accepted solution for LeetCode #1164: Product Price at a Given Date
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1164: Product Price at a Given Date
# Write your MySQL query statement below
WITH
T AS (SELECT DISTINCT product_id FROM Products),
P AS (
SELECT product_id, new_price AS price
FROM Products
WHERE
(product_id, change_date) IN (
SELECT product_id, MAX(change_date) AS change_date
FROM Products
WHERE change_date <= '2019-08-16'
GROUP BY 1
)
)
SELECT product_id, IFNULL(price, 10) AS price
FROM
T
LEFT JOIN P USING (product_id);
"#
}
// Accepted solution for LeetCode #1164: Product Price at a Given Date
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1164: Product Price at a Given Date
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1164: Product Price at a Given Date
// # Write your MySQL query statement below
// WITH
// T AS (SELECT DISTINCT product_id FROM Products),
// P AS (
// SELECT product_id, new_price AS price
// FROM Products
// WHERE
// (product_id, change_date) IN (
// SELECT product_id, MAX(change_date) AS change_date
// FROM Products
// WHERE change_date <= '2019-08-16'
// GROUP BY 1
// )
// )
// SELECT product_id, IFNULL(price, 10) AS price
// FROM
// T
// LEFT JOIN P USING (product_id);
// "#
// }
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.