Skip to content

50. Latest Event at or Before a Time

Difficulty: Medium · Topic: DSA for data engineers

Exchange rates are recorded at sorted times. Write as_of(times, values, queries): times is sorted ascending (it may contain repeats) and values[i] was recorded at times[i]. For each query time q, return the value of the latest record with time <= q; for equal times, the later record wins. If no record is that early, return None.

Return one answer per query, in query order. With 100,000 records and 100,000 queries, it must be O(log n) per query.

Your code runs in CPython on our servers against 5 tests, including hidden ones.

Sample test

as_of([10, 20, 30], [1.1, 1.2, 1.3], [5, 10, 25, 99])

Expected: [None, 1.1, 1.2, 1.3]

Hints

Hint 1bisect.bisect_right(times, q) is the number of records with time <= q.
Hint 2The answer is the record just before that position.
Hint 3bisect_right also lands after equal times, which picks the later record.

Learn the concepts

More Python problems

All Python problems