Watch 10 video solutions for Not Boring Movies, a easy level problem involving Database. This walkthrough by Learn With Chirag has 9,251 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Cinema
+----------------+----------+ | Column Name | Type | +----------------+----------+ | id | int | | movie | varchar | | description | varchar | | rating | float | +----------------+----------+ id is the primary key (column with unique values) for this table. Each row contains information about the name of a movie, its genre, and its rating. rating is a 2 decimal places float in the range [0, 10]
Write a solution to report the movies with an odd-numbered ID and a description that is not "boring".
Return the result table ordered by rating in descending order.
The result format is in the following example.
Example 1:
Input: Cinema table: +----+------------+-------------+--------+ | id | movie | description | rating | +----+------------+-------------+--------+ | 1 | War | great 3D | 8.9 | | 2 | Science | fiction | 8.5 | | 3 | irish | boring | 6.2 | | 4 | Ice song | Fantacy | 8.6 | | 5 | House card | Interesting | 9.1 | +----+------------+-------------+--------+ Output: +----+------------+-------------+--------+ | id | movie | description | rating | +----+------------+-------------+--------+ | 5 | House card | Interesting | 9.1 | | 1 | War | great 3D | 8.9 | +----+------------+-------------+--------+ Explanation: We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 is boring so we do not include it in the answer.
Problem Overview: The cinema table contains movie records with id, movie, description, and rating. Return only movies whose id is odd and whose description is not 'boring'. The result must be sorted by rating in descending order.
Approach 1: SQL Query Using Simple Filtering (O(n log n) time, O(1) extra space)
This is the direct database solution. Filter rows with two conditions: id % 2 = 1 (odd IDs) and description != 'boring'. After filtering, apply ORDER BY rating DESC to sort the result. The database scans rows and performs a sort, giving roughly O(n log n) time due to ordering. This approach relies on standard SQL filtering and works cleanly in ORMs such as SQLAlchemy.
Approach 2: Query Using SQL Conditions (Entity Framework) (O(n log n) time, O(1) extra space)
ORM frameworks like Entity Framework translate high‑level conditions into SQL. You express the same logic using conditional filters: select rows where id % 2 != 0 and description != "boring", then order by rating descending. The ORM generates the SQL query and pushes computation to the database engine. Complexity remains O(n log n) due to sorting, while the query benefits from database indexes if present.
Approach 3: Divide and Conquer (O(n log n) time, O(n) space)
If the dataset is loaded into memory (for example in a service layer), you can split the list of movies into halves, filter each half recursively for valid rows, and merge the results. During the merge step, apply a descending sort by rating similar to merge sort. The divide‑and‑conquer structure keeps filtering independent per segment, and the final merge step maintains ordering. This resembles techniques used in divide and conquer algorithms.
Approach 4: Dynamic Programming (Tabulation) (O(n log n) time, O(n) space)
A tabulation approach processes rows sequentially and stores valid results in a table or list. For each movie, check two conditions: odd ID and non‑boring description. Valid rows are appended to the DP table, which represents the accumulated filtered set. After processing all rows, sort the stored entries by rating in descending order. While DP is unnecessary for this problem, the pattern demonstrates incremental filtering and aggregation often used in dynamic programming workflows.
Recommended for interviews: The straightforward SQL filtering approach is what interviewers expect. It shows you understand conditional selection (WHERE) and result ordering (ORDER BY). Alternative approaches like divide‑and‑conquer or DP demonstrate algorithmic thinking when data is handled outside the database, but the optimal production solution keeps filtering inside the SQL query.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL Query Using Simple Filtering | O(n log n) | O(1) | Best approach when querying directly from a database using SQL |
| Query Using SQL Conditions (ORM) | O(n log n) | O(1) | When using frameworks like Entity Framework or SQLAlchemy |
| Divide and Conquer | O(n log n) | O(n) | When the dataset is processed in memory and parallel filtering is desired |
| Dynamic Programming (Tabulation) | O(n log n) | O(n) | When building filtered results incrementally before final sorting |