Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Python and SQL Coding Round

HardPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

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.

Constraints

  • 0 <= len(logs) <= 10^5
  • 1 <= k <= 10^4
  • 0 <= timestamp <= 10^9
  • Each event contains user, title, event, and timestamp
  • event is either play or stop
  • A completed session always has a nonnegative duration after sorting

Function Signature

def rank_titles(logs, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output