Back to questions

Average Post Hiatus (Part 2) Facebook SQL Interview Question

Average Post Hiatus (Part 2)

Facebook SQL Interview Question

You're given a table containing Facebook posts from users who have posted at least twice. Write a query to determine the average number of days between each user’s posts specifically during 2021.

Output user ID along with the average number of days (rounded to the nearest integer) between their posts.

Effective June 8th, 2023, the problem statement and solution have been updated.

Table:

Column NameType
user_idinteger
post_idinteger
post_datetimestamp
post_contenttext

Example Input:

user_idpost_idpost_datepost_content
00423978425407/04/2021 11:00:00Happy 4th of July!
15165259941507/10/2021 12:00:00Need a hug
15165211176607/12/2021 19:00:00I'm so done with covid - need travelling ASAP!
15165299415607/15/2021 12:00:00Does anyone have an extra iPhone charger to sell?
66109344256007/08/2021 14:00:00Just going to cry myself to sleep after watching Marley and Me.
66109362435607/29/2021 13:00:00Bed. Class 8-12. Work 12-3. Gym 3-5 or 6. Then class 6-10. Another day that's gonna fly by. I miss my girlfriend

Example Output:

user_idavg_hiatus_days
1516523
66109321

Explanation: Let's consider User 151652 as an example. Between their first and second post, there is a time difference of 2 days. Similarly, between their second and third post, there is a time difference of 3 days. By calculating the average of these time differences, we find that the average number of days between User 661903's posts rounded to the nearest integer is 3 days.

The dataset you are querying against may have different input & output - this is just an example!

Input

Output