The clipping part I got pretty quickly, GREATEST and LEAST to bound start and end to the day window.
Break the problem into stages using CTEs: first clip intervals to the target date, then merge overlapping/adjacent intervals per user, then compute gaps between merged intervals to check if any exceeds 60 minutes, and finally return either the total duration or NULL. Use window functions like LAG and cumulative sums to identify merge groups and gaps without procedural code.
Pro tip: Explicitly state your assumptions about interval boundaries (inclusive/exclusive) and how you handle edge cases like intervals exactly 60 minutes apart or touching intervals, as these details often determine correctness in production.
Filter and adjust activity intervals so they fall within the specified day, using GREATEST and LEAST to clip start and end times to the day's boundaries.
Use a gaps-and-islands technique: order intervals by start time, flag new groups when the start exceeds the running maximum end, then aggregate to get merged intervals.
For each user, compute the difference between the start of each merged interval and the end of the previous one; if any gap > 60 minutes, mark the user for NULL output.
Sum the durations of merged intervals for users without large gaps, floor to whole minutes, and return NULL for users with any gap > 60 minutes.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.