Quick Overview

Given cache items with insertion times and TTLs, count how many are active at each query time, where each item lives on a half-open interval. Tests whether you can answer many point queries faster than checking every item, and whether you handle items inserted or expiring exactly at the query time.

Count active cache items at each query time from insertion times and TTLs

Company: Zscaler

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D integer array `cache` where `cache[i] = [insertion_time, ttl]` represents a cache item that was inserted at time `insertion_time` with a time-to-live (TTL) of `ttl` time units. A cache item is considered active at time `q` if `insertion_time <= q < insertion_time + ttl`. You are also given an integer array `queries` where each element is a query time. For each query time, return the number of cache items that are active at that time. ### Function Signature ```python def count_active_items(cache: list[list[int]], queries: list[int]) -> list[int]: ``` ### Rules - An item is active from its insertion time inclusive up to `insertion_time + ttl` exclusive. - Return one count per query, in the same order as `queries`. ### Constraints - `1 <= len(cache) <= 10^4` - `len(cache[i]) == 2` - `0 <= cache[i][0] <= 10^9` - `1 <= cache[i][1] <= 10^9` - `1 <= len(queries) <= 10^4` - `0 <= queries[j] <= 10^9` ### Examples ```text Input: cache = [[1, 5], [2, 3], [6, 2]], queries = [3, 5, 7] Output: [2, 1, 1] ``` The items are active on [1, 6), [2, 5) and [6, 8). At time 3 the first two are active. At time 5 only [1, 6) is active, because [2, 5) has just expired. At time 7 only [6, 8) is active. ```text Input: cache = [[0, 10], [0, 10], [0, 10]], queries = [0, 5, 9, 10] Output: [3, 3, 3, 0] ``` All three items are active on [0, 10), so times 0, 5 and 9 each see 3 items. At time 10 all of them have expired.

Overview: Given cache items with insertion times and TTLs, count how many are active at each query time, where each item lives on a half-open interval. Tests whether you can answer many point queries faster than checking every item, and whether you handle items inserted or expiring exactly at the query time.

You are given a 2D integer array `cache` where `cache[i] = [insertion_time, ttl]` describes a cache item that was inserted at time `insertion_time` with a time-to-live (TTL) of `ttl` time units. A cache item is active at time `q` if `insertion_time <= q < insertion_time + ttl`. The insertion time is inclusive and the expiry time `insertion_time + ttl` is exclusive, so an item no longer counts at the exact moment it expires. You are also given an integer array `queries` where each element is a query time. For each query time, count the cache items that are active at that time. Return a list with one count per query, in the same order as `queries`. Query times may repeat and may appear in any order, and several cache items may be identical; each item is counted on its own. Implement `count_active_items(cache, queries)`. ### Example 1 ```text Input: cache = [[1, 5], [2, 3], [6, 2]], queries = [3, 5, 7] Output: [2, 1, 1] ``` The items are active on [1, 6), [2, 5) and [6, 8). At time 3 the first two are active. At time 5 only [1, 6) is active, because [2, 5) has just expired. At time 7 only [6, 8) is active. ### Example 2 ```text Input: cache = [[0, 10], [0, 10], [0, 10]], queries = [0, 5, 9, 10] Output: [3, 3, 3, 0] ``` All three items are active on [0, 10), so times 0, 5 and 9 each see 3 items. At time 10 all of them have expired. ### Constraints - `1 <= len(cache) <= 10^4` - `len(cache[i]) == 2` - `0 <= cache[i][0] <= 10^9` - `1 <= cache[i][1] <= 10^9` - `1 <= len(queries) <= 10^4` - `0 <= queries[j] <= 10^9` The expiry time `insertion_time + ttl` can reach 2 * 10^9. That is below 2^31 - 1 = 2,147,483,647, so every input value, expiry time and count fits in a signed 32-bit integer.

Constraints

  • 1 <= len(cache) <= 10^4
  • len(cache[i]) == 2
  • 0 <= cache[i][0] <= 10^9
  • 1 <= cache[i][1] <= 10^9
  • 1 <= len(queries) <= 10^4
  • 0 <= queries[j] <= 10^9
  • insertion_time + ttl <= 2 * 10^9 follows from the bounds above and fits in a signed 32-bit integer

Examples

Input: ([[1, 5], [2, 3], [6, 2]], [3, 5, 7])

Expected Output: [2, 1, 1]

Explanation: First source example: [2, 5) has just expired at time 5, and [6, 8) is the only item active at 7.

Input: ([[0, 10], [0, 10], [0, 10]], [0, 5, 9, 10])

Expected Output: [3, 3, 3, 0]

Explanation: Second source example: three identical items active on [0, 10), all expired at time 10.

Hints

  1. Trace the first example at time 5: the item inserted at time 2 with ttl 3 no longer counts, while an item inserted exactly at a query time does count.
  2. The answers must follow the order of queries, even though query times can repeat and arrive in any order.
  3. With up to 10^4 items and 10^4 queries, look for a way to prepare the cache once so that each query costs less than a full scan of every item.

Count Active Cache Items at Each Query Time

mediumZscalerSoftware Engineer

You are given a 2D integer array cache where cache[i] = [insertion_time, ttl] describes a cache item that was inserted at time insertion_time with a time-to-live (TTL) of ttl time units.

A cache item is active at time q if insertion_time <= q < insertion_time + ttl. The insertion time is inclusive and the expiry time insertion_time + ttl is exclusive, so an item no longer counts at the exact moment it expires.

You are also given an integer array queries where each element is a query time. For each query time, count the cache items that are active at that time. Return a list with one count per query, in the same order as queries. Query times may repeat and may appear in any order, and several cache items may be identical; each item is counted on its own.

Implement count_active_items(cache, queries).

Example 1

Input: cache = [[1, 5], [2, 3], [6, 2]], queries = [3, 5, 7]
Output: [2, 1, 1]

The items are active on [1, 6), [2, 5) and [6, 8). At time 3 the first two are active. At time 5 only [1, 6) is active, because [2, 5) has just expired. At time 7 only [6, 8) is active.

Example 2

Input: cache = [[0, 10], [0, 10], [0, 10]], queries = [0, 5, 9, 10]
Output: [3, 3, 3, 0]

All three items are active on [0, 10), so times 0, 5 and 9 each see 3 items. At time 10 all of them have expired.

Constraints

  • 1 <= len(cache) <= 10^4
  • len(cache[i]) == 2
  • 0 <= cache[i][0] <= 10^9
  • 1 <= cache[i][1] <= 10^9
  • 1 <= len(queries) <= 10^4
  • 0 <= queries[j] <= 10^9

The expiry time insertion_time + ttl can reach 2 * 10^9. That is below 2^31 - 1 = 2,147,483,647, so every input value, expiry time and count fits in a signed 32-bit integer.

Examples

Example 1
Input
([[1, 5], [2, 3], [6, 2]], [3, 5, 7])
Output
[2, 1, 1]
Notes
First source example: [2, 5) has just expired at time 5, and [6, 8) is the only item active at 7.
Example 2
Input
([[0, 10], [0, 10], [0, 10]], [0, 5, 9, 10])
Output
[3, 3, 3, 0]
Notes
Second source example: three identical items active on [0, 10), all expired at time 10.

Constraints

  • •1 <= len(cache) <= 10^4
  • •len(cache[i]) == 2
  • •0 <= cache[i][0] <= 10^9
  • •1 <= cache[i][1] <= 10^9
  • •1 <= len(queries) <= 10^4
  • •0 <= queries[j] <= 10^9
  • •insertion_time + ttl <= 2 * 10^9 follows from the bounds above and fits in a signed 32-bit integer

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_active_items(cache: list[list[int]], queries: list[int]) -> list[int]:
Sample call
count_active_items([[1, 5], [2, 3], [6, 2]], [3, 5, 7])
Language
Case 1
Input
([[1, 5], [2, 3], [6, 2]], [3, 5, 7])
Expected
[2, 1, 1]
Case 2
Input
([[0, 10], [0, 10], [0, 10]], [0, 5, 9, 10])
Expected
[3, 3, 3, 0]

Browse More Questions

Show the approach

Approach

An item is active at time q exactly when its insertion time is at most q and its expiry time insertion_time + ttl is greater than q. Since ttl >= 1, an item whose expiry time is at most q was also inserted at or before q. So the items inserted by time q split into two groups, those still active and those already expired, and the active count equals (number of insertion times <= q) minus (number of expiry times <= q).

The algorithm sorts the insertion times and the expiry times into two separate arrays. For each query it runs two binary searches that return how many values in each array are <= q, and writes the difference at that query's position in the output. Queries are never reordered, so the answers come back in the original query order.

Both boundary rules follow from the same <= comparison: an insertion time equal to q is counted as started (inclusive start), and an expiry time equal to q is counted as expired (exclusive end). Identical items each contribute their own insertion and expiry time, so duplicates are counted separately, and a repeated query gets the same answer each time. A query earlier than every insertion time gives 0 - 0 = 0. The largest expiry time is 2 * 10^9, which fits in a signed 32-bit integer; the Java and C++ references still compute it in 64-bit arithmetic.

Time complexity:
O((n + m) log n), where n = len(cache) and m = len(queries)
Space complexity:
O(n + m) for the two sorted arrays and the output