Group timestamped values into fixed-size time buckets sorted by bucket start

Quick Overview

Group unordered timestamp and value pairs into fixed-size time buckets aligned to multiples of the bucket size, and return the buckets in ascending order of start time. It tests hash map grouping, aligning timestamps down to a bucket boundary, and keeping values in input order inside each bucket.

Group timestamped values into fixed-size time buckets sorted by bucket start

Company: Revolut

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an unordered list `entries`, where each entry is a pair `[timestamp, value]`, and an integer `bucket_size`. Group the entries into fixed-size time buckets. Each bucket spans exactly `bucket_size` seconds. A bucket's start timestamp is found by aligning the timestamp to the nearest lower multiple of `bucket_size`. Formally, for a timestamp `t`, the bucket start is `(t // bucket_size) * bucket_size`. Return a list of buckets sorted by their start timestamp. Each bucket is represented as `[bucket_start, values]`, where `values` contains all values whose timestamps fall into that bucket, in the order they appear in the input. ### Function Signature ```python def bucket_time_series(entries: list[list[int]], bucket_size: int) -> list[list]: ``` ### Rules - Each entry is a list of two integers, `[timestamp, value]`. - A bucket covers the half-open range `[bucket_start, bucket_start + bucket_size)`, so a timestamp that is an exact multiple of `bucket_size` belongs to the bucket that starts at that timestamp. - Only buckets that contain at least one entry appear in the output. - Inside a bucket, values keep their input order, even when that differs from timestamp order. Entries with equal timestamps also keep input order. - Buckets are sorted by `bucket_start` ascending. Bucket starts are distinct, so the output is unique. ### Constraints - `1 <= len(entries) <= 10^5` - `0 <= entries[i][0] <= 10^9` - `-10^4 <= entries[i][1] <= 10^4` - `1 <= bucket_size <= 10^6` ### Examples ```text Input: entries = [[0, 10], [3, 20], [5, 30], [11, 40], [7, 50]], bucket_size = 5 Output: [[0, [10, 20]], [5, [30, 50]], [10, [40]]] ``` Timestamps 0 and 3 fall into bucket `[0, 5)`, timestamps 5 and 7 into `[5, 10)`, and timestamp 11 into `[10, 15)`. ```text Input: entries = [[100, 1], [200, 2], [150, 3], [250, 4], [120, 5]], bucket_size = 100 Output: [[100, [1, 3, 5]], [200, [2, 4]]] ``` Timestamps 100, 150 and 120 all fall into the bucket starting at 100, and their values stay in input order (1, 3, 5) even though 120 is earlier than 150. Timestamps 200 and 250 fall into the bucket starting at 200. ```text Input: entries = [[12, 7], [3, -2], [47, 5], [10, 1], [3, 4]], bucket_size = 10 Output: [[0, [-2, 4]], [10, [7, 1]], [40, [5]]] ``` Both entries at timestamp 3 go to bucket 0 in input order. Timestamps 12 and 10 go to bucket 10, with 7 before 1 because the entry at 12 comes first in the input. Buckets 20 and 30 have no entries, so they are left out.

Overview: Group unordered timestamp and value pairs into fixed-size time buckets aligned to multiples of the bucket size, and return the buckets in ascending order of start time. It tests hash map grouping, aligning timestamps down to a bucket boundary, and keeping values in input order inside each bucket.

Revolut logo
Revolut
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an unordered list entries, where each entry is a pair [timestamp, value], and an integer bucket_size. Group the entries into fixed-size time buckets. Each bucket spans exactly bucket_size seconds.

A bucket's start timestamp is found by aligning the timestamp to the nearest lower multiple of bucket_size. Formally, for a timestamp t, the bucket start is (t // bucket_size) * bucket_size.

Return a list of buckets sorted by their start timestamp. Each bucket is represented as [bucket_start, values], where values contains all values whose timestamps fall into that bucket, in the order they appear in the input.

Function Signature

def bucket_time_series(entries: list[list[int]], bucket_size: int) -> list[list]:

Rules

  • Each entry is a list of two integers, [timestamp, value] .
  • A bucket covers the half-open range [bucket_start, bucket_start + bucket_size) , so a timestamp that is an exact multiple of bucket_size belongs to the bucket that starts at that timestamp.
  • Only buckets that contain at least one entry appear in the output.
  • Inside a bucket, values keep their input order, even when that differs from timestamp order. Entries with equal timestamps also keep input order.
  • Buckets are sorted by bucket_start ascending. Bucket starts are distinct, so the output is unique.

Constraints

  • 1 <= len(entries) <= 10^5
  • 0 <= entries[i][0] <= 10^9
  • -10^4 <= entries[i][1] <= 10^4
  • 1 <= bucket_size <= 10^6

Examples

Input: entries = [[0, 10], [3, 20], [5, 30], [11, 40], [7, 50]], bucket_size = 5
Output: [[0, [10, 20]], [5, [30, 50]], [10, [40]]]

Timestamps 0 and 3 fall into bucket [0, 5), timestamps 5 and 7 into [5, 10), and timestamp 11 into [10, 15).

Input: entries = [[100, 1], [200, 2], [150, 3], [250, 4], [120, 5]], bucket_size = 100
Output: [[100, [1, 3, 5]], [200, [2, 4]]]

Timestamps 100, 150 and 120 all fall into the bucket starting at 100, and their values stay in input order (1, 3, 5) even though 120 is earlier than 150. Timestamps 200 and 250 fall into the bucket starting at 200.

Input: entries = [[12, 7], [3, -2], [47, 5], [10, 1], [3, 4]], bucket_size = 10
Output: [[0, [-2, 4]], [10, [7, 1]], [40, [5]]]

Both entries at timestamp 3 go to bucket 0 in input order. Timestamps 12 and 10 go to bucket 10, with 7 before 1 because the entry at 12 comes first in the input. Buckets 20 and 30 have no entries, so they are left out.