Find Inclusive Index Ranges for Time-Window Queries on Timestamp-Sorted Logs
AttentiveSoftware Engineer · Technical ScreenLast updated
Given log timestamps sorted in non-decreasing order with possible duplicates, answer many inclusive time-window queries by returning the first and last index of the entries inside each window, where a window may extend past the data's time range. It tests O(log n) per-query boundary searches on sorted data with duplicate keys.
A service stores the timestamps of its log entries in an array sorted in non-decreasing order, so entry i has timestamp timestamps[i]. The same log is queried many times, and each query asks which entries fall inside a time window [start, end].
For each query, return the inclusive index range [first, last] of the log entries whose timestamps lie inside the window. Several entries may share the same timestamp, both ends of the window are inclusive, and the window may begin before the earliest entry or end after the latest one.
Function Signature
def log_window_ranges(timestamps: list[int], queries: list[list[int]]) -> list[list[int]]:
Each query is a pair [start, end].
Rules
- Entry
iis in the window whenstart <= timestamps[i] <= end. - For each query,
firstis the smallest index andlastis the largest index of an entry in the window. Becausetimestampsis sorted, every index fromfirsttolastis in the window. - If no entry falls in the window, the answer for that query is
[-1, -1]. startandendneed not be timestamps that occur intimestamps, and either may lie before the first entry or after the last one.- Return one
[first, last]pair per query, in the same order asqueries.
Constraints
0 <= len(timestamps) <= 10^51 <= len(queries) <= 10^5timestampsis sorted in non-decreasing order; values may repeat.0 <= timestamps[i] <= 10^90 <= start <= end <= 2 * 10^9for every query; all values fit in a 32-bit signed integer.- Answer each query in $O(\log n)$ time, where $n$ is
len(timestamps); scanning the log for every query is too slow at these limits.
Examples
Example 1
Input: timestamps = [1, 3, 3, 5, 8], queries = [[3, 5], [0, 100], [6, 7]]
Output: [[1, 3], [0, 4], [-1, -1]]
For [3, 5], both entries at timestamp 3 (indices 1 and 2) and the entry at timestamp 5 (index 3) are inside the inclusive window. [0, 100] begins before the first entry and ends after the last one, so it covers the whole log. No entry has a timestamp from 6 to 7.
Example 2
Input: timestamps = [2, 2, 2, 4, 4, 9], queries = [[2, 2], [3, 4], [4, 20], [0, 1], [10, 15]]
Output: [[0, 2], [3, 4], [3, 5], [-1, -1], [-1, -1]]
[2, 2] returns the whole run of entries at timestamp 2. [3, 4] starts at a timestamp that does not occur in the log. [4, 20] ends after the latest entry. [0, 1] lies entirely before the earliest entry and [10, 15] entirely after the latest, so both are empty.
Example 3
Input: timestamps = [], queries = [[0, 10]]
Output: [[-1, -1]]
An empty log has no entries in any window.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.