Design Movie Rental System - Problem

๐ŸŽฌ Movie Rental Empire Challenge

You're tasked with building the backend system for a movie rental empire with n shops across the city! Your system needs to handle the core operations that keep customers happy: searching for movies, renting them out, returning them, and generating business reports.

Each movie copy is represented as [shop_id, movie_id, price] where:

  • shop_id: Which shop has this copy
  • movie_id: The movie identifier
  • price: Rental price for this specific copy

Important: Each shop can only have one copy of any given movie.

Your Mission:

  1. search(movie) - Find the 5 cheapest shops with available copies (unrented)
  2. rent(shop, movie) - Rent out a specific movie from a specific shop
  3. drop(shop, movie) - Return a rented movie to its shop
  4. report() - Get the 5 cheapest currently rented movies for business analytics

All results must be sorted by price first, then by shop ID, then by movie ID for ties! ๐ŸŽฏ

Input & Output

example_1.py โ€” Basic Operations
$ Input: MovieRentingSystem(3, [[0,1,5],[0,2,6],[0,3,7],[1,1,4],[1,2,7],[2,1,5]]) search(1) -> rent(0,1) -> rent(1,2) -> report() -> drop(1,2) -> search(2)
โ€บ Output: [1,0,2] -> [] -> [] -> [[0,1],[1,2]] -> [] -> [0,2]
๐Ÿ’ก Note: Initially movie 1 available at shops 1($4), 0($5), 2($5) - return [1,0,2]. After renting, report shows cheapest rented movies. After dropping, movie 2 becomes available at shop 0.
example_2.py โ€” Tie Breaking
$ Input: MovieRentingSystem(2, [[0,1,5],[1,1,5],[0,2,3],[1,2,8]]) search(1) -> rent(0,1) -> search(1)
โ€บ Output: [0,1] -> [] -> [1]
๐Ÿ’ก Note: Both shops have movie 1 at $5, but shop 0 comes first (smaller shop ID). After renting from shop 0, only shop 1 has it available.
example_3.py โ€” Empty Results
$ Input: MovieRentingSystem(1, [[0,1,3]]) search(2) -> rent(0,1) -> search(1) -> report()
โ€บ Output: [] -> [] -> [] -> [[0,1]]
๐Ÿ’ก Note: Movie 2 doesn't exist, so search returns empty. After renting movie 1, it's no longer available for search, but shows up in report.

Constraints

  • 1 โ‰ค n โ‰ค 3 ร— 105
  • 1 โ‰ค entries.length โ‰ค 105
  • 0 โ‰ค shopi < n
  • 1 โ‰ค moviei, pricei โ‰ค 105
  • Each shop carries at most one copy of movie moviei
  • All calls to rent and drop are valid
  • At most 105 calls in total to search, rent, drop, and report
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen