Table: Problems
+-------------+------+ | Column Name | Type | +-------------+------+ | problem_id | int | | likes | int | | dislikes | int | +-------------+------+ In SQL, problem_id is the primary key column for this table. Each row of this table indicates the number of likes and dislikes for a LeetCode problem.
Find the IDs of the low-quality problems. A LeetCode problem is low-quality if the like percentage of the problem (number of likes divided by the total number of votes) is strictly less than 60%.
Return the result table ordered by problem_id in ascending order.
The result format is in the following example.
Example 1:
Input: Problems table: +------------+-------+----------+ | problem_id | likes | dislikes | +------------+-------+----------+ | 6 | 1290 | 425 | | 11 | 2677 | 8659 | | 1 | 4446 | 2760 | | 7 | 8569 | 6086 | | 13 | 2050 | 4164 | | 10 | 9002 | 7446 | +------------+-------+----------+ Output: +------------+ | problem_id | +------------+ | 7 | | 10 | | 11 | | 13 | +------------+ Explanation: The like percentages are as follows: - Problem 1: (4446 / (4446 + 2760)) * 100 = 61.69858% - Problem 6: (1290 / (1290 + 425)) * 100 = 75.21866% - Problem 7: (8569 / (8569 + 6086)) * 100 = 58.47151% - Problem 10: (9002 / (9002 + 7446)) * 100 = 54.73006% - Problem 11: (2677 / (2677 + 8659)) * 100 = 23.61503% - Problem 13: (2050 / (2050 + 4164)) * 100 = 32.99002% Problems 7, 10, 11, and 13 are low-quality problems because their like percentages are less than 60%.
Problem Overview: The table Problems stores the number of likes and dislikes for each problem. A problem is considered low quality if its like ratio is below 0.6. The task is to return the problem_id of every problem where likes / (likes + dislikes) < 0.6.
Approach 1: Direct Ratio Filter (O(n) time, O(1) space)
The most direct solution computes the like ratio for every row and filters the rows where the ratio is below 0.6. In SQL, iterate through the table with a simple SELECT query and apply the condition likes / (likes + dislikes) < 0.6 inside the WHERE clause. The database engine performs a linear scan of the table and evaluates the ratio for each record.
This works because each row already contains all the required information. No joins, grouping, or window functions are required. The query simply performs arithmetic and filtering. The time complexity is O(n) since every row is checked once, and the space complexity is O(1) because no additional structures are created.
This approach is the most readable and commonly used in SQL interview questions. It clearly communicates the business rule: identify rows where the like ratio falls below the quality threshold.
Approach 2: Cross-Multiplication Comparison (O(n) time, O(1) space)
Instead of performing division, you can transform the ratio condition mathematically. The expression likes / (likes + dislikes) < 0.6 can be rewritten as likes < 0.6 * (likes + dislikes). This avoids explicit division and relies only on multiplication and comparison.
Many SQL engineers prefer this pattern when dealing with ratios because it avoids potential floating‑point precision issues. The query still scans the table once and evaluates the inequality for each row. The complexity remains O(n) time and O(1) space.
This technique is common in database filtering tasks where ratios or percentages determine eligibility. Transforming the equation makes the query easier for some query planners to optimize and can be slightly more robust when dealing with numeric types.
Recommended for interviews: Use the direct ratio filter first because it clearly expresses the requirement and shows you understand the problem statement. If the interviewer asks about numeric precision or query optimization, mention the cross‑multiplication alternative. Demonstrating both approaches shows comfort with SQL arithmetic and filtering logic in real production queries.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Ratio Filter | O(n) | O(1) | Best for readability when directly applying the ratio condition in SQL |
| Cross-Multiplication Comparison | O(n) | O(1) | Useful when avoiding floating point division or improving numeric stability |
My Brain after 569 Leetcode Problems • NeetCode • 2,927,647 views views
Watch 9 more video solutions →Practice Low-Quality Problems with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor