You are given tables/logs from a consumer app. Solve the following independent tasks.
Part A — Active following as of a date (SQL)
You have a follow event log:
-
follow_events(follower_id, followee_id, event_type, event_ts)
-
event_type ∈ {'follow','unfollow'}
-
A user can follow/unfollow the same person multiple times.
Task: Given a parameter date D (inclusive), return all active follow relationships at the end of day D (i.e., after all events with event_ts <= D 23:59:59).
Output columns:
-
as_of_date
(value =
D
),
follower_id
,
followee_id
Clarifications/edge cases to handle:
-
If the latest event on or before
D
is
follow
, the relationship is active.
-
If the latest event is
unfollow
, it is inactive.
-
If there are no events on/before
D
, it should not appear.
Part B — Add reciprocal friendships (SQL)
You have a directed friendship table:
-
friendships(user_id, friend_id)
A friendship should be bidirectional (if (A,B) exists, (B,A) should also exist).
Task: Produce the set of missing reciprocal rows that must be inserted to make the data bidirectional, without creating duplicates.
Output columns: user_id, friend_id representing rows to insert.
Part C — Mutual friends (Python)
You are given an undirected friendship list edges, where each element is a pair (u, v) meaning u and v are friends.
Task: Implement a function that returns the set (or sorted list) of mutual friends of two users a and b.
Constraints to consider:
-
The graph can be large; aim for an efficient approach.
Part D — Valid car bookings (Python)
A single car can be booked by only one customer at a time.
You are given booking requests as a list of tuples (booking_id, start_time, end_time), where start_time < end_time.
Rule: Accept bookings in increasing start_time order; a booking is valid/accepted if it does not overlap with any previously accepted booking.
Task: Return the list of accepted booking_ids.
Edge cases:
-
Back-to-back bookings where
end_time == next_start_time
do
not
overlap.
-
Input may be unsorted.