Maintain a Priced Movie Loan Catalog

Quick Overview

Maintain a Priced Movie Loan Catalog. Implement MovieRentingSystem for n shops and entries [shop,movie,price], with at most one copy of each movie per shop. Initially every copy is available. search(movie) returns at most five available shop IDs ordered by price, then shop ID. rent(shop,movie) moves an available copy into the rented inventory; drop reverses that move. report() returns at most five rented [shop,movie] pairs ordered by price, then shop ID, then movie ID.

Maintain a Priced Movie Loan Catalog

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Implement the persistent `MovieRentingSystem` class for `n` shops. Each `entries` row is `[shop,movie,price]`, with at most one copy of a movie at a shop. All copies start unrented. `search(movie)` returns up to five shops with an unrented copy, ordered by ascending price and then shop ID. `rent(shop,movie)` rents that available copy, and `drop(shop,movie)` returns that rented copy. `report()` returns up to five rented `[shop,movie]` pairs, ordered by ascending price, then shop ID, then movie ID. Return all matches when fewer than five exist, or an empty list when there are none. ```cpp class MovieRentingSystem { public: MovieRentingSystem(int n, vector<vector<int>>& entries); vector<int> search(int movie); void rent(int shop, int movie); void drop(int shop, int movie); vector<vector<int>> report(); }; ``` ### Examples **Example 1:** ```text n = 2, entries = [[0,7,4],[1,7,3],[0,8,3]] Calls: search(7), rent(0,8), rent(1,7), report(), search(7) Results: [1,0], void, void, [[0,8],[1,7]], [0] Explanation: The two rentals tie in price; shop 0 is reported first. ``` **Example 2:** ```text n = 1, entries = [[0,2,6]] Calls: rent(0,2), search(2), report(), drop(0,2), report() Results: void, [], [[0,2]], void, [] Explanation: Returning the copy restores availability and clears the rental report. ``` ### Constraints - `1 <= n <= 3 * 10^5`; `1 <= entries.length <= 10^5`. - Shops lie in `[0,n-1]`; movie IDs and prices lie in `[1,10^4]`. - Each shop/movie pair is unique. - At most `10^5` method calls in total. - rent is called only for an available existing copy; drop only for a currently rented copy. ### Clarifying Questions The constructor and methods share one persistent object. The examples describe method invocations; they do not replace the original class API. Price never changes. ```hint Inspect the structure Which ordered keys express the two different tie-breaking rules? ``` ### Reasoning checkpoints - Apply the distinct search and report tie-breaking rules, including equal prices and copies of one movie at different shops. - Keep availability and rentals consistent across a valid sequence of operations. - Preserve the native constructor, method arguments and return values. ### Optional extensions How would supporting rental-price changes affect the system?

Overview: Maintain a Priced Movie Loan Catalog. Implement MovieRentingSystem for n shops and entries [shop,movie,price], with at most one copy of each movie per shop. Initially every copy is available. search(movie) returns at most five available shop IDs ordered by price, then shop ID. rent(shop,movie) moves an available copy into the rented inventory; drop reverses that move. report() returns at most five rented [shop,movie] pairs ordered by price, then shop ID, then movie ID.

Okta logo
Okta
May 29, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement the persistent MovieRentingSystem class for n shops. Each entries row is [shop,movie,price], with at most one copy of a movie at a shop. All copies start unrented. search(movie) returns up to five shops with an unrented copy, ordered by ascending price and then shop ID. rent(shop,movie) rents that available copy, and drop(shop,movie) returns that rented copy. report() returns up to five rented [shop,movie] pairs, ordered by ascending price, then shop ID, then movie ID. Return all matches when fewer than five exist, or an empty list when there are none.

class MovieRentingSystem {
public:
    MovieRentingSystem(int n, vector<vector<int>>& entries);
    vector<int> search(int movie);
    void rent(int shop, int movie);
    void drop(int shop, int movie);
    vector<vector<int>> report();
};

Examples

Example 1:

n = 2, entries = [[0,7,4],[1,7,3],[0,8,3]]
Calls: search(7), rent(0,8), rent(1,7), report(), search(7)
Results: [1,0], void, void, [[0,8],[1,7]], [0]
Explanation: The two rentals tie in price; shop 0 is reported first.

Example 2:

n = 1, entries = [[0,2,6]]
Calls: rent(0,2), search(2), report(), drop(0,2), report()
Results: void, [], [[0,2]], void, []
Explanation: Returning the copy restores availability and clears the rental report.

Constraints

  • 1 <= n <= 3 * 10^5 ; 1 <= entries.length <= 10^5 .
  • Shops lie in [0,n-1] ; movie IDs and prices lie in [1,10^4] .
  • Each shop/movie pair is unique.
  • At most 10^5 method calls in total.
  • rent is called only for an available existing copy; drop only for a currently rented copy.

Clarifying Questions Guidance

The constructor and methods share one persistent object. The examples describe method invocations; they do not replace the original class API. Price never changes.

Reasoning checkpoints

  • Apply the distinct search and report tie-breaking rules, including equal prices and copies of one movie at different shops.
  • Keep availability and rentals consistent across a valid sequence of operations.
  • Preserve the native constructor, method arguments and return values.

Optional extensions

How would supporting rental-price changes affect the system?