FreeExam

Intervals and Event Processing

Thirty original Python problems on interval boundaries, atomic timestamp batches, periodic support, interpolation, allocation and event-order evidence. Preserve exact measures, missing support and deterministic witnesses while avoiding coordinate-width expansion.

These problems are original practice. They are not questions from a named employer or a live hiring loop.

  1. 1.Unite intervals without erasing an excluded boundary point

    Write mixed_union(intervals), [lo,hi,left_closed,right_closed], integer lo<=hi and bool flags. Empty point intervals are ignored unless both ends closed. Return sorted maximal union intervals in the same form. Touching intervals merge iff at least one includes their shared point; two open ends leave a singleton gap. Preserve closure of outer endpoints. No mutation.

  2. 2.Preserve owner support across atomic interval boundaries

    Write owner_atoms(rows), [owner,start,end], integer start<=end, intervals [start,end). Return maximal [start,end,sorted_active_owners] spans with nonempty support. Same owner may have overlapping/repeated rows; ending one does not end its other support. Apply all changes at a timestamp together. Merge adjacent spans only when owner lists agree. Empty intervals ignored. No mutation.

  3. 3.Integrate strict overload across atomic boundaries

    Write quota_excess(rows,capacity), [start,end,nonnegative_load], integer start<=end, capacity>=0. Demand is summed on half-open intervals. Return [integral_of_max(demand-capacity,0),peak_demand,maximal_overload_spans], overload iff demand>capacity. Apply same-time endpoints atomically; empty intervals contribute nothing. Adjacent overloaded spans merge even if demand changes. No mutation.

  4. 4.Extract odd occurrence support across shared boundaries

    Write odd_windows(intervals), integer half-open [start,end), start<=end. A point is selected iff covered by an ODD number of input occurrences. Duplicate intervals count separately. Return sorted maximal selected spans. Empty intervals ignored; same-time endpoint toggles applied together. Adjacent selected spans coalesce if parity stays odd across the boundary. No mutation.

  5. 5.Map mixed intervals to signed lattice-index support

    Write lattice_support(intervals,origin,step), step>0 integer, intervals [lo,hi,left_closed,right_closed], integer lo<=hi. Grid points are origin+k*step for integer k of either sign. Return [maximal_closed_k_ranges,total_distinct_points], merging adjacent k ranges. Respect open endpoints and empty point intervals. Do not enumerate points; bounds may be large. No mutation.

  6. 6.Join every positive overlap with source identities

    Write interval_join(left,right), rows [unique_id,start,end] per source, integer start<=end, half-open intervals. Return sorted [left_id,right_id,overlap_start,overlap_end] for EVERY positive-length overlap. Empty/touching intervals do not join. Intervals within either source may overlap; input order is arbitrary. IDs are unique only within their source. No mutation.

  7. 7.Resolve unsorted active queries with complete priority ties

    Write active_choice(rows,queries), [unique_id,start,end,priority], integer start<=end, half-open spans. Per query time choose active row by smallest priority, then latest start, then lexical ID; return ID or None in original query order. Empty spans never active. Queries may repeat or go backwards; do not expire rows in original query order. No mutation.

  8. 8.Normalize wrapped interval support on one period

    Write circular_union(period,arcs), integer period>0, arcs [start,duration], start any integer, 0<=duration<=period. Each arc covers [start,start+duration) modulo period. Return canonical sorted merged linear spans in [0,period), merging touching spans; wrapped support may remain two boundary spans. Full-period duration covers everything; zero duration covers nothing. No mutation.

  9. 9.Intersect noncoprime schedules above a signed bound

    Write simultaneous_tick(schedules,not_before), [phase,positive_period] integers. Find smallest integer t>=not_before with (t-phase)%period==0 for EVERY schedule, or None if incompatible. Periods need not be coprime; phases and bound may be negative. Empty schedule list returns not_before. Do not scan the time horizon. No mutation.

  10. 10.Count periodic ticks outside merged blackout support

    Write unblocked_ticks(phase,period,start,end,blackouts), integer period>0, start<=end; blackouts half-open integer [lo,hi), lo<=hi. Ticks phase+k*period, k any integer, must lie in [start,end) outside every blackout. Return [count,first_tick_or_None,last_tick_or_None]. Overlapping blackouts do not subtract twice. Avoid scanning ticks or coordinate width. No mutation.

  11. 11.Apply resource deltas as all-or-nothing timestamp batches

    Write atomic_balances(initial,events): nonnegative integer balances; events [integer_time,resource,integer_delta]. Process times sorted, net deltas per resource. Reject WHOLE time if any balance would be negative; otherwise apply together. Missing resources start at 0, appearing only when an accepted batch mentions them. Return [final_copy,accepted_times,rejected_times]. No input mutation.

  12. 12.Read atomic same-time writes without hiding typed conflicts

    Write epoch_snapshot(initial,events): [time,set,key,JSON_value] or [time,get,key]; integer times/numbers. Same-time writes precede reads. Equal typed structural writes coalesce; conflicts reject ALL writes at that time, reads see prior state. None is a value. Return [reads,rejected_times,final_copy]; reads [input_index,found,value_or_None] in input order. Deep detach outputs; no input mutation.

  13. 13.Integrate only supported linear anchor spans exactly

    Write linear_integrals(points,windows): unique integer [time,value] anchors; integer windows [start,end], start<=end. Sort anchors; interpolate linearly between neighbors only, without extrapolation. Return per window [supported_duration,unsupported_duration,reduced_integral_num_den]. Integrate supported parts only; empty/one-anchor support has zero integral. Preserve window order. No mutation.

  14. 14.Measure strict linear threshold exposure and equal plateaus

    Write linear_exposure(points,threshold), unique integer [time,value] anchors, integer threshold. Interpolate linearly between sorted neighbors; no extrapolation. Return [duration_strictly_above,duration_equal], each reduced [num,den], over the anchor hull. Nonflat crossing points have zero equal-duration; flat equal plateaus count. Fewer than two anchors gives two zero fractions. No mutation.

  15. 15.Invert a piecewise nonnegative work rate

    Write work_finish(segments,begin,work), disjoint integer [start,end,nonnegative_rate], start<=end, work>=0. Gaps and time outside segments have rate 0. Accumulate from integer begin. Return earliest completion time as reduced [num,den], or None if total remaining work insufficient. Zero work completes at begin; exact segment-end completion precedes any following gap. No mutation.

  16. 16.Find the entire weighted interval-distance minimizer range

    Write interval_median(rows), [lo,hi,positive_integer_weight], integer lo<=hi. For integer x minimize sum(weight*distance(x,[lo,hi])), closed intervals. Return [[leftmost_minimizer,rightmost_minimizer],minimum_cost], representing every integer between them; None for empty rows, whose minimizers are unbounded. Signed bounds/point intervals allowed. Do not enumerate coordinate width. No mutation.

  17. 17.Minimize integer stabbing count then its lexical witness

    Write stab_points(intervals), <=10 half-open integer [lo,hi), lo<=hi. Choose distinct integer points hitting EVERY interval. Return smallest point count, then lexical sorted point list; return the list itself. Any empty interval makes it impossible (None); empty interval family gives []. Bounds may be huge, so do not enumerate coordinate width. No mutation.

  18. 18.Paint clipped ranges while retaining clear spans

    Write paint_segments(length,updates): integer length>=0; [lo,hi,label], integer lo<=hi, string/None labels. Initially None on [0,length). Apply updates in order, clipped to domain; None clears. Return maximal sorted [start,end,label] spans covering the domain, including clear gaps. Ignore empty clipped updates. Avoid coordinate-width expansion. Zero length returns []. No mutation.

  19. 19.Replay lowest-aligned free-block allocations by identity

    Write allocate_segments(total,operations): integer total>=0. Ops [allocate,id,positive_size,positive_alignment] or [free,id]. Allocate lowest aligned offset fitting [0,total) without active overlap. Active duplicate ID, no fit or unknown free rejects unchanged. IDs reusable after free. Return [successful [op_index,offset],rejected_indices,sorted active [id,offset,size]]. No input mutation.

  20. 20.Integrate signed range additions in input event order

    Write range_events(events), [add,lo,hi,integer_delta] or [sum,lo,hi], integer lo<=hi. Initially zero everywhere. Add changes value on [lo,hi); sum returns its current integral over [lo,hi). Process input order, return sum answers only. Signed deltas/bounds allowed; empty ranges contribute zero. Avoid iterating coordinate width, which may be huge. No mutation.

  21. 21.Pair each response with one latest supported prior event

    Write causal_pairs(events,max_lag), [unique_id,side_a_or_b,key,integer_time], max_lag>=0. Process by time, then all a before b, then ID. Each b pairs with unused same-key a having 0<=b.time-a.time<=max_lag, latest a time then lexical a ID. Pair consumes a. Return [pairs_in_b_processing_order,sorted_unmatched_a_IDs,sorted_unmatched_b_IDs]. No mutation.

  22. 22.Count patterns with strictly increasing timestamps

    Write pattern_count(events,pattern), [integer_time,kind] occurrences, pattern list of kinds. Count choices of distinct occurrences spelling pattern with STRICTLY increasing times, irrespective of input order. Equal-time events cannot occupy different stages of one match; duplicate occurrences count separately. Empty pattern has one match. Return exact integer count. No mutation.

  23. 23.Sessionize neighboring gaps while preserving identities

    Write session_runs(events,gap), [unique_id,key,integer_time], gap>=0. Per key sort by time then ID. Neighboring events share a session iff their gap<=gap, transitively; do not measure from session start. Return [key,first_time,last_time,chronological_IDs], sorted by key then session start. Same-time occurrences remain distinct. Last time is last observed event, not an expiry. No mutation.

  24. 24.Buffer frontier-equal events before strict prior emissions

    Write bounded_reorder(events,lag), [unique_id,integer_time] arrival order, lag>=0. Before each arrival, reject if time<existing frontier. Otherwise insert, update max_seen and frontier=max_seen-lag, emit buffered events with time<frontier sorted by time/ID. Equality stays buffered/allowed. At end flush remaining time/ID order. Return [emitted_IDs,late_input_indices]. No mutation.

  25. 25.Retain an input subsequence with feasible increasing times

    Write ordered_support(intervals), <=12 closed integer [lo,hi], lo<=hi. Select a subsequence in input order and assign each selected occurrence an integer within its range, strictly increasing. Maximize count, then lexical input-index list, then lexical assigned times. Return [indices,times]. Bounds may be huge; do not enumerate time values. Empty input returns [[],[]]. No mutation.

  26. 26.Measure rectangle union area and boundary perimeter

    Write rectangle_union(rectangles), integer [x1,y1,x2,y2], x1<=x2,y1<=y2, half-open boxes. Return [union_area,union_perimeter], counting hole boundaries. Ignore zero-area boxes; duplicates do not multiply support. Shared edges are internal, point contacts remove no boundary length. Coordinates may be large: compress endpoints, do not enumerate unit coordinates. No mutation.

  27. 27.Integrate periodic overlap through a signed hyperperiod

    Write periodic_exposure(first,second,start,end). Each schedule [phase,positive_period,on_duration], integer 0<=on<=period; start<=end integers. Active at real t iff (t-phase)%period<on. Return total continuous overlap duration in [start,end). LCM periods<=2000, horizon may be huge/negative. Use finite hyperperiod support, not horizon expansion. No mutation.

  28. 28.Choose the smallest simultaneous overload witness

    Write overload_witness(rows,capacity): <=12 [unique_id,start,end,nonnegative_load], integer start<=end, capacity>=0, half-open spans. Choose simultaneously active rows whose summed load>capacity. Minimize subset size, then earliest witness time, then lexical sorted IDs. Return [time,IDs,total_load] or None. Earliest common support is max starts; empty intersection cannot witness. No mutation.

  29. 29.Compute a contained-work bound and canonical window

    Write demand_bound(jobs): integer [release,deadline,nonnegative_work], release<=deadline. Ignore zero work; positive zero-width work => None. For positive-work release a and deadline b, a<b, maximize sum(work with release>=a and deadline<=b)/(b-a); tie earliest a then b. Return [reduced_fraction,[a,b]], or [[0,1],None] without positive work. This defines a bound, not a schedule. No mutation.

  30. 30.Repair interval capacity while preserving pinned identities

    Write repair_capacity(rows,capacity,pinned), <=12 [unique_id,start,end,nonnegative_cost], integer start<=end, capacity>=0; pinned IDs declared. Keep pins; remove others so at every real time at most capacity remaining half-open intervals are active. Empty spans consume nothing. Minimize (removed_cost,removed_count,lexical_sorted_removed_IDs). Return [cost,IDs] or None if impossible. No mutation.