Top K Movies by Play Count from CSV Data
Updated
Problem statement
A movie service returns its catalog as CSV data. The response has already been fetched and split into lines for you: lines[0] is the header title,author,play_count,duration, and every other line describes one movie with those four fields, separated by commas.
The data is not written consistently, so each field must be decoded:
- A field may be written as-is. It then contains no comma and no double quote.
- A field may instead be wrapped in double quotes. Inside the quotes a comma is an ordinary character, and each double quote in the value is written twice (
""). The outer quotes are not part of the value. - A quoted
play_countmay use commas as thousands separators, such as"1,234,567". Without quotes it is plain digits.
Return the titles of the k movies with the most plays, the most played first. When two movies have the same play count, the one whose title is smaller in string order (comparing character codes, so uppercase letters come before lowercase) comes first. If there are fewer than k movies, return all of them in that order.
Function
topKMovies(lines: String[], k: int) → String[]Examples
Example 1
lines = ["title,author,play_count,duration","Night Train,Ana Ruiz,5200,95","\"Hello, World\",Bo Chen,\"12,000\",88","Big Day,Cy Diaz,800,102","\"The \"\"Last\"\" Stop\",Di Evans,5200,77"]k = 2return = ["Hello, World","Night Train"]"Hello, World"is quoted because the title has a comma. Its play count"12,000"is quoted too and means12000, the most plays.Night TrainandThe "Last" Stopboth have5200plays. Inside quotes,""stands for one", so the second title isThe "Last" Stop.- The tie goes to the smaller title in string order, and
"N"comes before"T", soNight Trainis second.
Example 2
lines = ["title,author,play_count,duration","Alpha,Ann Lee,10,60","\"Beta\",Ann Lee,\"7\",61"]k = 5return = ["Alpha","Beta"]There are only two movies, so both are returned even though k = 5. The quotes around Beta and 7 are not part of the values.
Example 3
lines = ["title,author,play_count,duration"]k = 1return = []The data has only the header line and no movies, so the answer is empty.
Constraints
1 <= lines.length <= 3011 <= k <= 300lines[0]is exactly"title,author,play_count,duration".- Every other line has exactly four fields in the format described above. A
titlehas 1 to 30 characters and anauthor1 to 20, using English letters, digits, spaces and the characters. ' ! & : ( ) - , ". An unquoted field never contains a comma or a double quote, and no value starts or ends with a space. - Each
play_countis a whole number from0to10^9without leading zeros. Eachdurationis a whole number from1to600, quoted or not, without separators. - All titles are different.
More Tubi Phone Screen questions
Similar questions at other companies
- Top N Frequent Words in a FileMeta · PHONE SCREENMeta interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Brazilian Receivables Part 3 — Partial ContractsStripe · PHONE SCREENStripe interview questions
- Sort Characters by How Often They AppearAdobe · PHONE SCREENAdobe interview questions
- Top Counted Word RecordsIndeed · PHONE SCREENIndeed interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- Find Valid IP AddressesVerkada · PHONE SCREENVerkada interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW