Top Track Sequence
0:00:00
Let’s say we work with Spotify listening data, and we want to know which runs of three tracks people play back to back most often. Write a function called most_common_sequence that returns the most common sequence of three consecutive tracks played by the same user, as a tuple, along with the number of times it occurred.
The input plays is a list of (user_id, track_id, timestamp) play events from all users, sorted by timestamp.
Rules
- Overlapping sequences each count, so one user playing A, B, C, A counts (A, B, C) and (B, C, A) once each.
- Track ids are strings.
- If several sequences tie, return the smallest tuple.
- Plays with the same timestamp keep their list order.
- If no user has three plays, return (None, 0).
Example:
Input:
plays = [
(1, "A", 1), (2, "A", 2), (1, "B", 3), (2, "B", 4),
(1, "C", 5), (2, "C", 6), (1, "D", 7), (2, "A", 8),
]
Output:
def most_common_sequence(plays) -> (("A", "B", "C"), 2)
.
.
.
.
Comments