Optimize Performance of Favorite App Feature

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-01

Problem Statement

designing a “Favorite” (or “Like”) feature for an app (e.g., e-commerce, social media, streaming). Users can mark items as favorites, view their favorite list, and the system should handle large-scale traffic efficiently. The focus is on data structures, API design, and performance optimization.

Functional Requirements

1.Add Favorite
•Users can mark an item as favorite.
•Duplicates should be avoided; a user can favorite an item only once.
2.Remove Favorite
•Users can remove an item from their favorites.
3.Get Favorite List
•Retrieve a list of all items a user has favorited.
•Support pagination and optional sorting (e.g., by recency, popularity).
4.Get Item Popularity
•Return the number of users who have favorited a specific item.

Non-Functional Requirements

•Scalability: Handle millions of users and favorites efficiently.
•Low Latency: API calls (add, remove, get) should respond quickly.
•Memory Efficiency: Use space-optimized data structures for storing favorites.
•Consistency: Ensure user favorites are consistent across devices.
•Thread Safety: Handle concurrent add/remove operations.

Edge Cases & Constraints

•Adding an already-favorited item → no effect or return error.
•Removing an unfavorited item → no effect or return error.
•Large users with thousands of favorites → optimize storage and retrieval.
•High-read, high-write scenarios → balance caching and DB consistency.

Design Hints

•Key Classes / Interfaces:
•User → represents a user, contains userID and metadata.
•Item → represents the item that can be favorited.
•FavoriteService → main API with methods addFavorite(user, item), removeFavorite(user, item), getFavorites(user), getItemPopularity(item).
•FavoriteStore → underlying storage (DB, cache) mapping users ↔ items.
•Data Structures:
•HashMap / Set → for per-user favorites for O(1) add/remove/check.
•Count Map → maintain item popularity efficiently.
•Design Patterns:
•Singleton → for FavoriteService if centralized.
•Observer (Optional) → notify analytics or recommendation systems on favorite updates.

Example Flow

1.User A favorites "Item123" → stored in user’s favorite set, popularity count incremented.
2.User B favorites "Item123" → popularity count updated.
3.User A requests favorite list → retrieve list from FavoriteStore, possibly paginated.
4.User A removes "Item123" → update both user favorites and popularity count.

Performance Optimizations

•Use caching (Redis) for frequently accessed favorite lists and item popularity.
•Batch updates to DB to reduce write overhead.
•Use sorted sets or priority queues if favorites need ranking by recency/popularity.

Evaluation Areas

•Clean class design and modular API modeling.
•Efficient data structure selection for add/remove/get operations.
•Handling concurrency and large-scale performance.
•Extensibility to support analytics, notifications, or recommendations.


Solution

Comments