Your question is Python and SQL Coding Round. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Netflix playback logs contain play and stop events for users watching titles. The logs may arrive out of chronological order. Compute completed watch time for each title and return the top k titles.
A completed session begins with a play event and ends with the next stop event for the same (user, title) pair. Ignore a stop event when that pair is not currently playing. Ignore repeated play events while the pair is already playing. Sessions still active at the end of the logs do not contribute any time or viewers.
Implement rank_titles(logs, k). logs is a list of dictionaries with integer timestamp values and string user, title, and event values. event is either play or stop. Return a list of at most k dictionaries in ranking order. Each result dictionary must contain title, total_seconds, and viewers.
Rank titles by descending total_seconds, then descending viewers, then ascending title alphabetically. Include only titles with at least one completed session.
def rank_titles(logs, k):