Time and Window Analysis
Original Python data analyst interviews on event and arrival time, rolling support, calendar boundaries, sessions, follow-up maturity, and interval exposure.
These problems are original practice. They are not questions from a named employer or a live hiring loop.
1.Anchor half-open event bins correctly before the origin
Write
origin_bins(times, origin, width). All inputs are integers, width>0. Bin j is [origin+j*width,origin+(j+1)*width), including negative j. Return[bin_start,event_count]for observed bins sorted by start. Each timestamp occurrence counts, even duplicates; no empty bins are emitted. Negative offsets use mathematical floor, not truncation toward zero. No mutation.2.Group aware timestamps by their UTC date
Write
utc_day_counts(timestamps). Valid strings YYYY-MM-DDTHH:MM±HH:MM have explicit fixed offsets; UTC conversion stays in valid years. Normalize instants to UTC, count their UTC dates, and return[YYYY-MM-DD,count]sorted by date. Repeated instants count repeatedly. Do not use unconverted date prefixes or the machine local timezone. No mutation.3.Split interval measures by exact overlap duration
Write
allocate_intervals(rows, origin, width). Rows[integer_start,integer_end,amount], start<end; amounts are finite exact decimals, possibly negative; origin integer,width>0 integer. Spread each amount uniformly on [start,end), then sum its proportional overlap in anchored width bins. Return[bin_start,total]sorted by start, including touched bins whose total is 0. No mutation.4.Classify pending arrivals before measuring lateness
Write
arrival_ledger(rows, asof, allowed_delay). Rows[unique_id,event_time,arrival_time], integers with arrival>=event;allowed_delay>=0. Arrival>asof is"pending"; otherwise arrival-event<=allowed_delayis"on_time", else"late". Return{"on_time":ids,"late":ids,"pending":ids}, each in input order. Unarrived events cannot be observed late. No mutation.5.Keep observed event counts separate from window completeness
Write
watermark_windows(windows, events, watermark). windows[unique_id,start,end]have integer start<end; events lists integer times, duplicates count. Return[id,count,complete]in window input order: count events with start<=time<end; complete iff end<=watermark. Overlapping windows count independently. Use the supplied watermark, not the latest observed event. No mutation.6.Build ordered sessions with a strict inactivity boundary
Write
event_sessions(rows, gap). Rows[user,time,unique_event_id], integer times; gap>=0. Sort each user by (time,id). Begin a new session iff time-previous_time>gap; equality stays together. Return[user,first_time,last_time,event_ids_in_order]sorted by user then session start. Duplicate timestamps remain separate events; ids break their order. No mutation.7.Exclude equal-time events from prior lookback support
Write
prior_counts(rows, width). Rows[key,time,unique_id], integer time,width>0 integer. For each row count events of the same key with time in [time-width,time), including duplicate earlier timestamps but excluding ALL equal-time rows. Return[id,count]in original row order. Input order does not define temporal precedence; do not count another key or mutate.8.Let missing values consume row-window positions
Write
rolling_rows(rows, k). Rows[key,time,unique_id,value_or_None], integer times and finite exact decimals; k>=1 integer. Sort each key by (time,id). For each row use its last k rows INCLUDING itself; missing values consume positions but not numeric support. Return[id,observed_count,mean_or_None]in original input order. This is a row window, not an elapsed-time window. No mutation.9.Recover counter deltas without negative reset increments
Write
counter_deltas(rows). Rows[key,time,counter>=0 integer]have unique (key,time), integer times. Sort by key/time. First delta per key isNone; later delta=current-previous if current>=previous, else current (a reset-on-decrease rule). Return[key,time,delta_or_None]. A flat counter gives 0; a fall to 0 is a reset with delta 0. No mutation.10.Bound forward-filled values by observed-state age
Write
bounded_fill(rows, queries, max_age). Rows[key,time,value_or_None], unique (key,time), integer times,max_age>=0; finite numeric values. For each[key,time]query choose latest non-Nonerow at or before time with age<=max_age. Return[value_or_None,source_time_or_None]per query in input order. Missing rows do not clear numeric state; future rows cannot fill backward. No mutation.11.Interpolate bracketing observations without extrapolation
Write
interpolate_queries(rows, queries). Rows[key,integer_time,value], unique (key,time), finite exact decimals. queries[key,integer_time]retain input order. Return the exact-time value, else linear interpolation between nearest earlier/later observations of that key. If either bracket is absent returnNone. Do not extrapolate or cross keys. Output one value_or_None per query. No mutation.12.Integrate known step states and retain unknown duration
Write
state_exposure(rows, start, end). Rows[key,time,value_or_None], unique (key,time), integer times,start<end; finite exact values. State holds until the next row;Noneand time before the first row are unknown. Over [start,end), return[key,known_duration,known_area,known_time_mean_or_None]sorted by observed key. Retain keys with no known overlap; area integrates value*duration. No mutation.13.Measure clipped interval union rather than interval sums
Write
union_uptime(groups, rows, start, end). Unique string groups; rows[known_group,a,b]have integer a<=b; start<=end integers. Union intervals per group after clipping to [start,end). Return[group,covered_duration,fraction_or_None]sorted by group; fraction=duration/(end-start),Noneif horizon length 0. Touching intervals do not create a gap; empty intervals add no duration. No mutation.14.Integrate simultaneous activity with idle exposure
Write
concurrency_exposure(intervals, start, end). Integer intervals[a,b], a<=b; start<=end. Each nonempty interval adds one active unit on [a,b); duplicate intervals add separate units. Clip to the horizon. Return[active_units,duration]for positive durations sorted by units, including idle 0 when present. Simultaneous starts/ends transition together. Zero horizon gives []. No mutation.15.Find an earliest maximum-area fixed-length load window
Write
peak_load(rows, start, end, width). Rows[a,b,rate>=0]use integer a<b and finite exact rates; integer 0<width<=end-start. Choose real window start t in[start,end-width], maximize sum(rate*overlap([a,b),[t,t+width))). Return{"start":earliest maximizing t,"area":maximum}. Breakpoints are integer under these inputs. Overlapping rates add; no rows gives start and area 0. No mutation.16.Use ISO week years at calendar-year boundaries
Write
iso_week_counts(dates). dates lists valid YYYY-MM-DD strings. An ISO week runs Monday-Sunday; week 1 contains the year's first Thursday. Return[iso_year,iso_week,count]sorted by (year,week). Use ISO week year, which can differ from the date's calendar year. Repeated dates count as separate observations. No mutation.17.Require mature follow-up for cohort-age retention
Write
cohort_retention(joins, events, ages, cutoff). Integer days. joins maps users to join days<=cutoff; events[user,day]repeat; ages>=0. Per cohort/age report[age,eligible,retained,rate_or_None]: eligible=cohort size ifjoin_day+age<=cutoff, else 0; retained counts eligible users active that exact day. Return[join_day,reports]by day, ages in input order. Ignore unknown users. No mutation.18.Find earliest feasible ordered stage prefixes
Write
funnel_prefixes(entities, stages, events). Unique entities/stages; events[known_entity,known_stage,integer_time]may repeat. Per entity choose earliest first-stage time, then earliest next-stage time>=previous chosen time. Stop at first missing stage. Return[entity,chosen_prefix_times,complete_bool]sorted by entity. Equal times may satisfy stages; empty stages is complete. No mutation.19.Keep immature nonreturns pending rather than negative
Write
return_status(starts, events, horizon, cutoff). starts maps users to integer departure times<=cutoff; events[user,time]; horizon>0. First return must satisfy departure<time<=min(departure+horizon,cutoff). Return[user,"returned",first_time]if found; else[user,"not_returned",None]if deadline<=cutoff, else[user,"pending",None]. Sort users; ignore unknown-user events. No mutation.20.Center covariance on exact lag-aligned pair support
Write
lag_covariance(rows, lag). Rows[key,series,time,value_or_None], series"a"/"b", unique (key,series,time), integer times/lag; finite exact values. Pair a at t with b at t+lag only when both observed. Return[key,N,mean_a,mean_b,population_covariance]sorted by key; three numeric summariesNoneif N=0. Both means use pair support; N=1 covariance is 0. No interpolation or mutation.21.Shift calendar months with explicit last-day clamping
Write
shift_months(dates, offset). Valid YYYY-MM-DD dates; offset is an integer calendar-month shift, target years in 1..9999. Shift each ORIGINAL year/month by offset, retaining its day if valid, otherwise clamp to the target month last day. Return ISO dates in input order. Shift directly, without repeated one-month clamping or fixed-day durations. No mutation.22.Count eligible weekdays in a half-open date range
Write
business_days(start, end, holidays). start/end and holidays are valid YYYY-MM-DD strings. Count dates d with start<=d<end, Monday-Friday, and d not in the supplied holiday set. Duplicate holidays count once; holidays outside the range or on weekends have no extra effect. Return 0 if start>=end. Use this supplied calendar only, not real-world holiday rules. No mutation.23.Subtract blackout unions from canonical availability
Write
available_spans(availability, blackouts, start, end). Each list holds integer half-open intervals[a,b], a<=b; start<=end. Return sorted maximal nonempty intervals inside [start,end) covered by any availability and no blackout. Merge overlaps/touching spans in the result. Duplicate or overlapping blackouts subtract coverage only once; zero-length intervals remove nothing. No mutation.24.Return positive-duration resource conflict witnesses
Write
conflict_pairs(rows). Rows[resource,unique_id,start,end], integer start<=end. Compare pairs on the same resource only. Conflict requires positive overlapmax(start_a,start_b)<min(end_a,end_b); touching or empty intervals do not conflict. Return[resource,lower_id,higher_id,overlap_start,overlap_end]sorted by resource and ids. Each pair appears once even if coordinates match. No mutation.25.Bound completion without marking immature records failed
Write
deadline_bounds(rows, horizon, cutoff). Rows[unique_id,start,done_or_None], integer times with start<=cutoff,start<=done<=cutoff.Nonemeans unfinished. horizon>0; D=start+horizon. Return dicton_time/missed/pending (input-order ids): done<=D / done late or unfinished D<=cutoff / otherwise; lower=on_time_count/N, upper=(on_time_count+pending_count)/N, bothNoneif N=0. No mutation.26.Reconcile distinct users across adjacent time windows
Write
window_churn(rows, boundary, width). Rows[user,integer_time]may repeat; width>0 integer. Previous window: [boundary-width,boundary); next: [boundary,boundary+width). Return {"entering":sorted next-only users,"retained":sorted users in both,"lost":sorted previous-only users}. Ignore outside rows; count users once per window. Boundary events belong only to next. No mutation.27.Share event measures across matching reporting windows
Write
window_credit(windows, rows). windows[unique_id,start,end], integer start<end; rows[integer_time,amount], finite exact decimals. Split each event amount equally across windows with start<=time<end; counts stay unsplit. Return {"windows":[id,credit,event_count]sorted by id,"unassigned_total":unmatched amount sum,"unassigned_count":unmatched row count}. Retain empty windows. No mutation.28.Find the shortest closed span meeting an event quota
Write
quota_span(times, k). times contains integer timestamps, duplicates are separate events; k>=1 integer. Return[left,right,right-left]for a shortest CLOSED span containing at least k events, ties by earliest left then right. Endpoints must be observed timestamps. ReturnNonewith fewer than k events. A span of duration 0 can contain multiple equal-time events. No mutation.29.Keep ordered peak and trough witnesses for a level decline
Write
level_declines(rows). Rows[key,integer_time,level], unique (key,time), finite exact levels. Maximizeearlier_level-later_levelwithearlier_time<=later_time; self-pairs allow decline 0. Return[key,max_decline,peak_time,trough_time]sorted by key. Break equal-decline ties by earliest peak time, then trough time. A later high level cannot explain an earlier trough. No mutation.