Interview conceptCoding & Algorithms

Event Stream And Interval Pay Computation

Asked of: Software Engineer

Last updated

What's being tested

These problems test event-stream interval aggregation: converting unordered start/stop events into active time segments, then applying rates, overlaps, and conditional multipliers correctly. You must demonstrate careful input validation, deterministic handling of malformed or duplicate events, exact monetary arithmetic, and clear O(n log n) algorithmic reasoning.

Patterns & templates

  • Sweep line — convert starts to +1 and ends to -1, sort timestamps, then charge each interval using the active count before applying the delta.

  • Interval aggregation — for consecutive timestamps t[i-1], t[i], contribution is (t[i] - t[i-1]) × active_orders × rate; skip zero-length segments.

  • Event pairing — maintain open_order_id → start_time; reject unmatched ends, duplicate starts, negative durations, and invalid event types explicitly.

  • Conditional rates — split intervals at every peak-window boundary, then apply the correct multiplier per segment rather than classifying only by start time.

  • Money arithmetic — represent cents as integers, such as 30 cents per minute; round only once at the final boundary using the stated rule.

  • Daily boundaries — intersect each delivery interval with [day_start, day_end); use half-open intervals to avoid double-counting midnight or adjacent segments.

  • Complexity target — sorting dominates at O(n log n) time and O(n) space; a hash map plus sorted events is appropriate for millions of records.

Common pitfalls

Pitfall: Charging each order independently can miscompute concurrent-rate rules; aggregate active count across sorted timestamps instead.

Pitfall: Using floating-point dollars introduces rounding errors; parse timestamps consistently and compute in integer cents or exact decimals.

Pitfall: Silently “fixing” malformed or duplicate events creates non-deterministic pay; define whether to reject, ignore, or deduplicate and explain why.

Practice these

The practice cards below cover the canonical variants—solve all of them and time yourself.

Practice questions

Related concepts