Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Histogram From Transaction Logs

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Histogram From Transaction Logs. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Intercom's event-processing service records user transactions with Unix timestamps. Given a collection of transaction logs and a time range, produce a histogram showing how many transactions occurred in each consecutive time bucket.

Formal Specification

Implement build_histogram(logs, start, end, bucket_size). logs is a list of dictionaries, each containing an integer timestamp in seconds. start and end are integer Unix timestamps defining the half-open interval [start, end). bucket_size is a positive integer number of seconds. Assume (end - start) is positive and evenly divisible by bucket_size.

Return a list of integers with one entry per bucket. The first entry counts timestamps in [start, start + bucket_size), the second counts timestamps in [start + bucket_size, start + 2 * bucket_size), and so on. Ignore logs whose timestamps are before start or at or after end. Preserve zero counts for empty buckets.

Constraints

  • 0 <= len(logs) <= 10^5
  • 0 <= start < end <= 10^12
  • 1 <= bucket_size <= 10^12
  • (end - start) % bucket_size == 0
  • Every log has exactly one integer timestamp field
  • The interval is half-open: [start, end)

Function Signature

def build_histogram(logs, start, end, bucket_size):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output