Design a Popularity Counter

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-04-16

Problem Statement

Design a Popularity Counter system that tracks and maintains the popularity (score or count) of various items in real-time. Examples include like counters, vote counts, trending topics, or video views.


Focus on class design, service interactions, concurrency handling, and performance optimization rather than distributed system scaling (which is HLD).


Functional Requirements (FR)


1.Increment/Decrement Count
•Increment the count for a given item (e.g., new like, view, or vote).
•Decrement count if needed (e.g., unlike, vote cancellation).
2.Query Popularity
•Get current count for an item.
•Get top-N popular items.
3.Time-Based Queries (Optional)
•Track popularity over time (e.g., trending in last 24 hours).
•Provide counts per day, week, or month.
4.Notifications / Triggers (Optional)
•Trigger alerts when an item crosses a threshold.


Non-Functional Requirements (NFR)


•Low Latency – Increment and retrieval operations should be fast.
•Thread Safety – Handle concurrent updates to counts safely.
•High Throughput – Support a large number of updates per second.
•Memory Efficiency – Efficiently store counts for potentially millions of items.
•Extensibility – Easily add new types of popularity metrics.


Core Entities & Attributes


Class Key Attributes / Methods

Item itemId, name, metadata (optional)

PopularityCounter itemId → count mapping, increment(itemId), decrement(itemId), getCount(itemId)

TopNTracker maxHeap or sorted set to maintain top-N popular items

TimeSeriesCounter (Optional) itemId → timestamped counts for trend tracking


Service Layer / APIs


•increment(itemId) → Increase the count by 1.
•decrement(itemId) → Decrease the count by 1.
•getCount(itemId) → Returns current count.
•getTopN(n) → Returns top-N popular items.
•getTrend(itemId, startTime, endTime) → Returns historical counts (optional).


Design Considerations


1.Data Structures
•HashMap for itemId → count mapping.
•Heap / PriorityQueue for top-N tracking.
•Optional: Time-series map for trending.
2.Concurrency
•Use atomic counters for thread-safe increments/decrements.
•Fine-grained locks or concurrent hash maps.
3.Optimizations
•Batch updates for extremely high update rates.
•Periodic recalculation for top-N instead of continuous sorting.
4.Trade-offs
•Accuracy vs performance for high-throughput increments.
•Memory usage for storing top-N or trending history.
•Real-time top-N vs slightly delayed top-N calculation.


Expectations in LLD


•Class Diagram – Include Item, PopularityCounter, TopNTracker, and optional TimeSeriesCounter.
•Service Logic – Show how counts are updated, queried, and top-N items are maintained.
•Design Patterns – Singleton for global counter instance; Observer for threshold notifications.
•Concurrency Handling – Thread-safe updates using atomic operations or locks.


✅ This LLD problem tests:


•OOP and class design skills
•Efficient real-time data structures
•Concurrency and correctness in high-throughput scenarios


Solution

Comments