CD Rental System
Learn this problemProblem statement
Design a small in-memory CD rental system.
You are given an initial list of CD inventory entries. Each entry is [shop, cd, price], meaning shop shop owns one copy of CD cd with rental price price.
The system then receives commands. Return one output string for each command.
Commands
SEARCH|cd: return all shops that currently have CDcdavailable, sorted by price ascending, then shop id ascending. Return the shop ids as a single space-separated string. If no shop has an available copy, returnEMPTY.RENT|shop|cd: rent CDcdfromshop. This succeeds only if that shop currently has an available copy of that CD. ReturnOKon success orINVALIDotherwise.UNRENT|shop|cd: return CDcdtoshop. This succeeds only if that CD was previously rented from that shop and has not already been returned. ReturnOKon success orINVALIDotherwise.
What the interview report shared
The interview report described an onsite Java and DSA/LLD round where the candidate had to design and implement a CD rental system. It shared the initial data shape [shop, cd, price], the search(cd) API sorted by price then shop id, and the rent(shop, cd) and unrent(shop, cd) APIs with availability checks. The report did not specify command serialization, exact return strings, or sample inputs.
How FastPrep adapted it
FastPrep turned the class-style interview task into a single command-processing function. The OK, INVALID, and EMPTY outputs are practice scaffolding based on the reported APIs, not exact original wording.
Function
processCdRentalCommands(entries: String[][], commands: String[]) → String[]Examples
Example 1
entries = [["1","10","5"],["2","10","4"],["1","20","6"]]commands = ["SEARCH|10","RENT|2|10","SEARCH|10","RENT|2|10","UNRENT|2|10","SEARCH|10"]return = ["2 1","OK","1","INVALID","OK","2 1"]Before any rental, CD 10 is available at shop 2 for price 4 and shop 1 for price 5, so shop 2 comes first. After renting from shop 2, only shop 1 remains available until the CD is returned.
Example 2
entries = [["3","7","8"],["4","7","8"],["5","8","2"]]commands = ["SEARCH|7","RENT|4|7","SEARCH|7","UNRENT|3|7","UNRENT|4|7","SEARCH|7","SEARCH|9"]return = ["3 4","OK","3","INVALID","OK","3 4","EMPTY"]Shops 3 and 4 have the same price for CD 7, so the smaller shop id comes first. Returning from shop 3 is invalid because that copy was never rented.
Constraints
1 <= entries.length <= 1000001 <= commands.length <= 100000- Each
[shop, cd]pair appears at most once inentries. 1 <= shop, cd, price <= 10^9- Commands are always one of
SEARCH,RENT, orUNRENTwith the field counts shown above.