Price at Query Timestamps
Problem statement
A price history is given as two arrays of the same length: from time times[i] on, the price is prices[i], until the next record takes over. The record times are strictly increasing.
You are also given an array queries of times, in any order. For each query time q, find the record with the largest time that is at most q and report its price. A record whose time equals q already applies at q. If q is earlier than every record time, no price is known yet; report the empty string "".
Examples
Example 1
times = [0,50,500]prices = ["Price A","Price B","Price C"]queries = [-10,20,50,499,600]return = ["","Price A","Price B","Price B","Price C"]- Time
-10is before the first record at time0, so no price is known yet:"". - Time
20: the latest record at or before it is time0, so"Price A". - Time
50matches a record exactly, so that record's"Price B"applies. - Time
499: the next record starts at500, so"Price B"still applies. - Time
600: the latest record is time500, so"Price C".
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.