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.
This approach involves using a straightforward SQL query to filter movies based on conditions specified in the question. We will look for movies with an ID that is odd and where the description is not 'boring'. Once filtered, results will be sorted according to the ratings in descending order.
We use SQLAlchemy to define a table 'Cinema' and insert the sample data. We query this table using conditions specified: id as odd and a non-'boring' description, finally ordering by ratings in descending order. The results are printed one by one.
Python (SQLAlchemy)
The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size.
This approach directly uses SQL logic to filter out the records based on conditions specified in the question: selecting only those entries with an odd-numbered ID and a non-boring description, followed by sorting the result set based on ratings in descending order.
We begin by defining a Movie class and a list of Movie objects, after which we filter through this list in a LINQ query to fetch movies with odd-numbered IDs while excluding descriptions marked 'boring'. Finally, results are ordered by rating before being printed.
C# (Entity Framework)
The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies.
This approach involves breaking down the problem into smaller sub-problems, solving each one individually, and then combining their solutions to solve the original problem. This is a powerful technique used in algorithms like Merge Sort and Quick Sort. It allows us to efficiently tackle the problem by utilizing recursive problem-solving.
This is a Merge Sort implementation in C, which follows the Divide and Conquer technique. The array is recursively split into halves until each sub-array has one element. Then, the sub-arrays are merged back together in a sorted manner. This algorithm is efficient for large datasets.
Time Complexity: O(n log n)
Space Complexity: O(n)
Dynamic programming is a technique used for solving complex problems by breaking them down into simpler subproblems. Tabulation involves solving the subproblems and storing their results in a table (usually an array) to avoid redundant calculations. This approach is efficient for problems where overlapping subproblems and optimal substructure properties exist.
This JavaScript function calculates the nth Fibonacci number using a bottom-up dynamic programming approach. It uses an array to store and reuse previously computed Fibonacci numbers, efficiently computing the result in O(n) time.
JavaScript
Java
Time Complexity: O(n)
Space Complexity: O(n)
We can use the WHERE clause to filter out the records where description is not boring and id is odd, and then use the ORDER BY clause to sort the result in descending order by rating.
MySQL
| Approach | Complexity |
|---|---|
| SQL Query Using Simple Filtering | The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size. |
| Query Using SQL Conditions | The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies. |
| Divide and Conquer | Time Complexity: O(n log n) |
| Dynamic Programming (Tabulation) | Time Complexity: O(n) |
| Conditional Filtering + Sorting | — |
| 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 |
Not Boring Movies | Leetcode 620 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 9,251 views views
Watch 9 more video solutions →Practice Not Boring Movies with our built-in code editor and test cases.
Practice on FleetCode