Design Movie Rental System - Problem

You have a movie renting company consisting of n shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies.

Each movie is given as a 2D integer array entries where entries[i] = [shop_i, movie_i, price_i] indicates that there is a copy of movie movie_i at shop shop_i with a rental price of price_i. Each shop carries at most one copy of a movie movie_i.

The system should support the following functions:

  • Search: Finds the cheapest 5 shops that have an unrented copy of a given movie. The shops should be sorted by price in ascending order, and in case of a tie, the one with the smaller shop_i should appear first.
  • Rent: Rents an unrented copy of a given movie from a given shop.
  • Drop: Drops off a previously rented copy of a given movie at a given shop.
  • Report: Returns the cheapest 5 rented movies as a 2D list where each entry is [shop, movie], sorted by price, then by shop, then by movie.

Input & Output

Example 1 — Basic Movie Rental Operations
$ Input: n = 3, entries = [[0,1,5],[0,2,6],[0,3,7],[1,1,4],[1,2,7],[2,1,5]], operations = [["search",1],["rent",1,1],["search",2],["report"]]
Output: [[1,0,2],[],[0],[1,1]]
💡 Note: Search movie 1: shops [1,0,2] by price [4,5,5]. Rent from shop 1. Search movie 2: shop [0]. Report: rented movie from shop 1 is [1,1].
Example 2 — Multiple Rent and Drop Operations
$ Input: n = 2, entries = [[0,1,1],[0,2,2],[1,1,3]], operations = [["search",1],["rent",0,1],["drop",0,1],["search",1]]
Output: [[0,1],[],[],[0,1]]
💡 Note: Initially movie 1 available at shops [0,1] with prices [1,3]. After rent and drop, same availability restored.
Example 3 — Report with Multiple Movies
$ Input: n = 2, entries = [[0,1,2],[1,1,3],[0,2,1]], operations = [["rent",0,1],["rent",1,1],["rent",0,2],["report"]]
Output: [],[],[],[[0,2],[0,1],[1,1]]
💡 Note: After renting 3 movies, report shows them sorted by price: $1 movie 2 from shop 0, $2 movie 1 from shop 0, $3 movie 1 from shop 1.

Constraints

  • 1 ≤ n ≤ 3 × 104
  • 1 ≤ entries.length ≤ 105
  • 0 ≤ shopi < n
  • 1 ≤ moviei, pricei ≤ 104
  • Each shop carries at most one copy of a movie
  • 1 ≤ operations.length ≤ 105

Visualization

Tap to expand
INPUT DATAALGORITHMRESULTSShop 0Movie 1: $5Shop 1Movie 1: $4Shop 2Movie 1: $5Movies across shopswith different prices1234Hash Map by Movie IDPriority QueueSort by: Price → ShopMovie 1: [Shop1($4), Shop0($5)]Search Movie 1:[1, 0, 2]Sorted by priceO(log n) timeKey Insight:Use hash maps to group movies and heaps to maintain price-sorted order for efficient O(log n) operationsTutorialsPoint - Design Movie Rental System | Hash Maps + Heaps
Asked in
Netflix 15 Amazon 12 Google 8 Microsoft 6
23.4K Views
Medium Frequency
~45 min Avg. Time
892 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