Solve the following two interview-style tasks.
Task 1: Top creators by likes
You are given a list of like events for a business-facing creator platform. Each event contains:
-
viewer_id
: the user who liked the content
-
creator_id
: the creator who received the like
-
created_at
: timestamp of the like
Implement a function that returns the top k creators ranked by total number of likes received.
Requirements:
-
Count all like events by
creator_id
.
-
Return the top
k
creators sorted by like count in descending order.
-
If two creators have the same like count, sort by
creator_id
in ascending order.
-
Discuss the time and space complexity.
-
Optimize the solution for the case where the number of creators is very large and
k
is much smaller than the number of creators.
Task 2: Consecutive login days in SQL
You are given a table user_logins:
user_logins(
user_id BIGINT,
login_date DATE
)
A user may have multiple login records on the same date.
Write a SQL query, or explain the SQL approach, to compute each user's longest streak of consecutive login days.
Requirements:
-
Treat multiple logins on the same date as one login day.
-
Consecutive days must differ by exactly one calendar day.
-
Return
user_id
and
longest_streak
.
-
Use window functions where appropriate.