Python coding

Price tracker with late corrections

Write a class PriceTracker that records a stock's price over time, where some records arrive late or turn out to be wrong.

  • update(time, price) records price at time. Times arrive in any order, and a time that was already recorded gets corrected: its new price replaces the old one.
  • current() returns the price recorded at the latest time seen so far.
  • highest() returns the highest price among all recorded times, counting only each time's newest price.
  • lowest() returns the lowest such price.

Times and prices are positive integers, and the three queries are only called after at least one update. Every operation must take O(log n) amortized time, where n is the number of updates.

The tests call the provided run_tracker(ops): it applies each operation in order and returns the list of results (None for update, shown as null).

Besides the tests, one large input (7,000 prices, 9,800 queries) with a ~500 ms limit (scaled to your device's speed): brute force won't pass.

  • run_tracker([["update",1,10],["update",2,5],["current"],["highest"],["lowest"],["update",1,3],["current"],["highest"],["lowest"]]) → [null,null,5,10,5,null,5,5,3]
  • run_tracker([["update",5,40],["update",2,70],["current"],["highest"],["update",2,20],["highest"],["lowest"]]) → [null,null,40,70,null,40,20]
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

A hash map from time to price handles corrections and current. The hard part is the max and min after a correction, without scanning every price.

Hint 2

Push every update into a max-heap and a min-heap as (price, time). Don't hunt down the outdated entry a correction leaves behind; let it stay buried.

Hint 3

On a query, check the heap's top against the map: if the map holds a different price for that time, the entry is stale, so pop it and look again.

Solution and why it works
pythonO(log n) amortized per operation · O(n), n = updates
# --- your code ---
import heapq


class PriceTracker:
    def __init__(self):
        self.price = {}                      # time -> its price after any correction
        self.last = 0                        # latest time seen
        self.high, self.low = [], []         # heaps of (-price, time) and (price, time)

    def update(self, time: int, price: int) -> None:
        self.price[time] = price
        self.last = max(self.last, time)
        heapq.heappush(self.high, (-price, time))   # older entries for this time stay, stale
        heapq.heappush(self.low, (price, time))

    def current(self) -> int:
        return self.price[self.last]

    def highest(self) -> int:
        while -self.high[0][0] != self.price[self.high[0][1]]:   # stale: corrected since
            heapq.heappop(self.high)
        return -self.high[0][0]

    def lowest(self) -> int:
        while self.low[0][0] != self.price[self.low[0][1]]:
            heapq.heappop(self.low)
        return self.low[0][0]


# --- provided: do not edit ---
def run_tracker(ops: list[list]) -> list:
    tracker = PriceTracker()
    return [getattr(tracker, name)(*args) for name, *args in ops]

The map from time to price is the truth; heaps only propose answers. A heap cannot delete from the middle, so a correction just pushes a new entry and leaves the old one stale. A query pops tops that disagree with the map until one agrees. Each entry is pushed once and popped at most once: O(log n) amortized, versus O(n) for scanning every price per query. The trap: a time corrected back to an earlier price makes its old entry valid again; checking by value handles it.