Design a Temperature Tracking Data Structure
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-01-22
Problem Statement
Designing a data structure that efficiently tracks temperature readings over time. The system should support insertion of new readings, querying statistics, and retrieving recent data. Focus is on object-oriented design, modularity, and efficient data access.
✅ Functional Requirements
1.Insertion of Temperature Readings
•Add a new temperature reading with a timestamp: insert(timestamp, temperature).
•Timestamps are strictly increasing or may arrive out of order (clarify assumption).
2.Querying Statistics
•Retrieve the maximum temperature so far.
•Retrieve the minimum temperature so far.
•Retrieve the average temperature over all readings or a specific time window.
•Optional: retrieve median temperature efficiently.
3.Recent Data Access
•Retrieve the latest temperature reading.
•Retrieve readings within a specific time interval.
4.Optional Features
•Support rolling window statistics (e.g., last 24 hours).
•Support alerting if temperature crosses a threshold.
🔐 Constraints & Assumptions
•Focus on LLD and data structure design, not on persistent storage.
•Temperature is represented as a float or double.
•Assume readings can be large in number, so time and space efficiency matters.
•Single-threaded environment (thread-safety optional).
🧱 Key Components & Classes
1.TemperatureReading Class
1
2class TemperatureReading {
3public:
4 double temperature;
5 long timestamp;
6
7 TemperatureReading(double temp, long ts) {
8 temperature = temp;
9 timestamp = ts;
10 }
11};1.TemperatureTracker Class
•Central class for storing and querying readings.
1
2class TemperatureTracker {
3private:
4 vector<TemperatureReading> readings;
5 double maxTemp;
6 double minTemp;
7 double sumTemp;
8public:
9 TemperatureTracker() {
10 maxTemp = std::numeric_limits<double>::lowest();
11 minTemp = std::numeric_limits<double>::max();
12 sumTemp = 0;
13 }
14
15 void insert(double temperature, long timestamp);
16 double getMax();
17 double getMin();
18 double getAverage();
19 TemperatureReading getLatest();
20 vector<TemperatureReading> getReadingsInRange(long startTs, long endTs);
21};1.Optional Enhancements
•MedianFinder using two heaps (min-heap and max-heap) for efficient median retrieval.
•SlidingWindowTracker for rolling statistics in a time window.
🔄 Example Workflow
1.Insert readings: insert(72.5, 1629900000), insert(75.0, 1629900600).
2.Query statistics: getMax() → 75.0, getMin() → 72.5, getAverage() → 73.75.
3.Retrieve recent reading: getLatest() → 75.0.
4.Query readings in range: getReadingsInRange(1629900000, 1629900600) → [72.5, 75.0].
🧪 Expected Deliverables
•Class diagram showing TemperatureReading and TemperatureTracker.
•Implementation of insert, getMax, getMin, getAverage, getLatest, and range queries.
•Test cases:
•Insert multiple readings and check max, min, average
•Retrieve latest reading
•Query readings in a specific time interval
•Optional: verify median calculation and sliding window statistics
💡 Discussion Points
•Efficient statistics calculation: maintain running sum, min, max, and median.
•Rolling window queries: deque or circular buffer to store recent readings.
•Optional: thread-safety for concurrent inserts and queries.
•Space optimization: consider storing aggregated stats instead of all readings for long-term storage.