Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.
Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.
Design an algorithm that:
Implement the StockPrice class:
StockPrice() Initializes the object with no price records.void update(int timestamp, int price) Updates the price of the stock at the given timestamp.int current() Returns the latest price of the stock.int maximum() Returns the maximum price of the stock.int minimum() Returns the minimum price of the stock.Example 1:
Input
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output
[null, null, null, 5, 10, null, 5, null, 2]
Explanation
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3.
// Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum(); // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.
Constraints:
1 <= timestamp, price <= 109105 calls will be made in total to update, current, maximum, and minimum.current, maximum, and minimum will be called only after update has been called at least once.Problem summary: You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp. Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record. Design an algorithm that: Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp. Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded. Finds the maximum price the stock has been based on the current records. Finds the minimum price the stock has been based on the current records. Implement the StockPrice class: StockPrice() Initializes the
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Design · Segment Tree
["StockPrice","update","update","current","maximum","update","maximum","update","minimum"] [[],[1,10],[2,5],[],[],[1,3],[],[4,2],[]]
time-based-key-value-store)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2034: Stock Price Fluctuation
class StockPrice {
private Map<Integer, Integer> d = new HashMap<>();
private TreeMap<Integer, Integer> ls = new TreeMap<>();
private int last;
public StockPrice() {
}
public void update(int timestamp, int price) {
if (d.containsKey(timestamp)) {
int old = d.get(timestamp);
if (ls.merge(old, -1, Integer::sum) == 0) {
ls.remove(old);
}
}
d.put(timestamp, price);
ls.merge(price, 1, Integer::sum);
last = Math.max(last, timestamp);
}
public int current() {
return d.get(last);
}
public int maximum() {
return ls.lastKey();
}
public int minimum() {
return ls.firstKey();
}
}
/**
* Your StockPrice object will be instantiated and called as such:
* StockPrice obj = new StockPrice();
* obj.update(timestamp,price);
* int param_2 = obj.current();
* int param_3 = obj.maximum();
* int param_4 = obj.minimum();
*/
// Accepted solution for LeetCode #2034: Stock Price Fluctuation
type StockPrice struct {
d map[int]int
ls *redblacktree.Tree
last int
}
func Constructor() StockPrice {
return StockPrice{
d: make(map[int]int),
ls: redblacktree.NewWithIntComparator(),
last: 0,
}
}
func (this *StockPrice) Update(timestamp int, price int) {
merge := func(rbt *redblacktree.Tree, key, value int) {
if v, ok := rbt.Get(key); ok {
nxt := v.(int) + value
if nxt == 0 {
rbt.Remove(key)
} else {
rbt.Put(key, nxt)
}
} else {
rbt.Put(key, value)
}
}
if v, ok := this.d[timestamp]; ok {
merge(this.ls, v, -1)
}
this.d[timestamp] = price
merge(this.ls, price, 1)
this.last = max(this.last, timestamp)
}
func (this *StockPrice) Current() int {
return this.d[this.last]
}
func (this *StockPrice) Maximum() int {
return this.ls.Right().Key.(int)
}
func (this *StockPrice) Minimum() int {
return this.ls.Left().Key.(int)
}
/**
* Your StockPrice object will be instantiated and called as such:
* obj := Constructor();
* obj.Update(timestamp,price);
* param_2 := obj.Current();
* param_3 := obj.Maximum();
* param_4 := obj.Minimum();
*/
# Accepted solution for LeetCode #2034: Stock Price Fluctuation
class StockPrice:
def __init__(self):
self.d = {}
self.ls = SortedList()
self.last = 0
def update(self, timestamp: int, price: int) -> None:
if timestamp in self.d:
self.ls.remove(self.d[timestamp])
self.d[timestamp] = price
self.ls.add(price)
self.last = max(self.last, timestamp)
def current(self) -> int:
return self.d[self.last]
def maximum(self) -> int:
return self.ls[-1]
def minimum(self) -> int:
return self.ls[0]
# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()
// Accepted solution for LeetCode #2034: Stock Price Fluctuation
/**
* [2034] Stock Price Fluctuation
*
* You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.
* Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.
* Design an algorithm that:
*
* Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
* Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
* Finds the maximum price the stock has been based on the current records.
* Finds the minimum price the stock has been based on the current records.
*
* Implement the StockPrice class:
*
* StockPrice() Initializes the object with no price records.
* void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
* int current() Returns the latest price of the stock.
* int maximum() Returns the maximum price of the stock.
* int minimum() Returns the minimum price of the stock.
*
*
* Example 1:
*
* Input
* ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
* [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
* Output
* [null, null, null, 5, 10, null, 5, null, 2]
* Explanation
* StockPrice stockPrice = new StockPrice();
* stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
* stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5].
* stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5.
* stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1.
* stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3.
* // Timestamps are [1,2] with corresponding prices [3,5].
* stockPrice.maximum(); // return 5, the maximum price is 5 after the correction.
* stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2].
* stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.
*
*
* Constraints:
*
* 1 <= timestamp, price <= 10^9
* At most 10^5 calls will be made in total to update, current, maximum, and minimum.
* current, maximum, and minimum will be called only after update has been called at least once.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/stock-price-fluctuation/
// discuss: https://leetcode.com/problems/stock-price-fluctuation/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
// Credit: https://leetcode.com/problems/stock-price-fluctuation/solutions/4501186/btreemap-hashset-and-hashmap/
struct StockPrice {
prices: std::collections::HashMap<i32, i32>,
curr: i32,
ord_prices: std::collections::BTreeMap<i32, i32>,
set: std::collections::HashSet<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl StockPrice {
fn new() -> Self {
Self {
prices: std::collections::HashMap::new(),
curr: 0,
ord_prices: std::collections::BTreeMap::new(),
set: std::collections::HashSet::new(),
}
}
fn update(&mut self, timestamp: i32, price: i32) {
if self.prices.contains_key(×tamp) {
let val = self.prices.get(×tamp).unwrap();
match self.ord_prices.get_mut(&val) {
Some(x) => {
if *x > 1 {
*x -= 1;
} else {
self.ord_prices.remove(&val);
self.set.remove(&val);
}
}
None => {}
}
}
if self.set.contains(&price) {
match self.ord_prices.get_mut(&price) {
Some(x) => {
*x += 1;
}
None => {}
}
} else {
self.ord_prices.insert(price, 1);
self.set.insert(price);
}
self.prices.insert(timestamp, price);
if timestamp > self.curr {
self.curr = timestamp;
}
}
fn current(&self) -> i32 {
self.prices.get(&self.curr).unwrap().clone()
}
fn maximum(&self) -> i32 {
self.ord_prices.last_key_value().unwrap().0.clone()
}
fn minimum(&self) -> i32 {
self.ord_prices.first_key_value().unwrap().0.clone()
}
}
/**
* Your StockPrice object will be instantiated and called as such:
* let obj = StockPrice::new();
* obj.update(timestamp, price);
* let ret_2: i32 = obj.current();
* let ret_3: i32 = obj.maximum();
* let ret_4: i32 = obj.minimum();
*/
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2034_example_1() {
let mut stock_price = StockPrice::new();
stock_price.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stock_price.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5].
assert_eq!(stock_price.current(), 5); // return 5, the latest timestamp is 2 with the price being 5.
assert_eq!(stock_price.maximum(), 10); // return 10, the maximum price is 10 at timestamp 1.
stock_price.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3.
// Timestamps are [1,2] with corresponding prices [3,5].
assert_eq!(stock_price.maximum(), 5); // return 5, the maximum price is 5 after the correction.
stock_price.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2].
assert_eq!(stock_price.minimum(), 2); // return 2, the minimum price is 2 at timestamp 4.
}
}
// Accepted solution for LeetCode #2034: Stock Price Fluctuation
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2034: Stock Price Fluctuation
// class StockPrice {
// private Map<Integer, Integer> d = new HashMap<>();
// private TreeMap<Integer, Integer> ls = new TreeMap<>();
// private int last;
//
// public StockPrice() {
// }
//
// public void update(int timestamp, int price) {
// if (d.containsKey(timestamp)) {
// int old = d.get(timestamp);
// if (ls.merge(old, -1, Integer::sum) == 0) {
// ls.remove(old);
// }
// }
// d.put(timestamp, price);
// ls.merge(price, 1, Integer::sum);
// last = Math.max(last, timestamp);
// }
//
// public int current() {
// return d.get(last);
// }
//
// public int maximum() {
// return ls.lastKey();
// }
//
// public int minimum() {
// return ls.firstKey();
// }
// }
//
// /**
// * Your StockPrice object will be instantiated and called as such:
// * StockPrice obj = new StockPrice();
// * obj.update(timestamp,price);
// * int param_2 = obj.current();
// * int param_3 = obj.maximum();
// * int param_4 = obj.minimum();
// */
Use this to step through a reusable interview workflow for this problem.
Use a simple list or array for storage. Each operation (get, put, remove) requires a linear scan to find the target element — O(n) per operation. Space is O(n) to store the data. The linear search makes this impractical for frequent operations.
Design problems target O(1) amortized per operation by combining data structures (hash map + doubly-linked list for LRU, stack + min-tracking for MinStack). Space is always at least O(n) to store the data. The challenge is achieving constant-time operations through clever structure composition.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.