Posts

LRU -MRU Cache Implementation

  Two Ways to Implement LRU Cache 1️⃣ Easy Way (using  LinkedHashMap ) In the code we just wrote with  LinkedHashMap ,  we don’t manually write a doubly linked list . That’s because  LinkedHashMap  internally already uses a doubly linked list  to maintain order (either insertion order or access order). So when we write:new LinkedHashMap<>(capacity, 0.75f, true) That  true  tells it to maintain the doubly linked list in  access order . We don’t see it in code because it’s hidden inside the  JDK implementation . 👉 This is why the  LinkedHashMap  solution is the  easy shortcut 2️⃣ Full Custom Way (HashMap + Doubly Linked List) In FAANG-style interviews, sometimes they say: “Don’t use LinkedHashMap, implement it yourself.” In that case, you  must build your own doubly linked list : HashMap<key, Node>  → fast lookup Node  → custom class with  key, value, prev, next head  → most recentl...

Designing Twitter -LeetCode

Image
 Requirements for Twitter System Design Blank diagram: Lucidchart 1 Functional Requirements: User Management A user can create an account, login/logout. Should be able to post new tweets (can be text, image, video etc). Should be able to follow other users. Should have a newsfeed feature consisting of tweets from the people the user is following. Should be able to search tweets. Tweets A user can post a tweet (limited to 280 characters). A user can delete their own tweet. Tweets should have a unique ID, text content, timestamp, and author. Timeline Each user should be able to view their timeline (tweets from the users they follow, ordered by most recent first). A user can also view their own tweets. Likes & Retweets (Optional Enhancement) A user can like/unlike a tweet. A user can retweet a tweet.  NonFunctional Requirements: High availability with minimal latency. Ths system should be scalable and efficient.  Extended Requirements: Metrices and analytics Retweet func...

How to Design a Rate Limiter API | Learn System Design

Image
How to Design a Rate Limiter API  A Rate Limiter API is a tool that developers can use to define rules that specify how many requests can be made in a given time period and what actions should be taken when these limits are exceeded.  Rate limiting   is an essential technique used in software systems to control the rate of incoming requests. It helps to prevent the overloading of servers by limiting the number of requests that can be made in a given time frame.  It helps to prevent a high volume of requests from overwhelming a server or API. Here is a basic design for a rate limiter API In this article, we will discuss the design of a rate limiter API, including its requirements, high-level design, and algorithms used for rate limiting. Why is rate limiting used? Avoid resource starvation due to a Denial of Service (DoS) attack. Ensure that servers are not overburdened. Using rate restriction per user ensures fair and reasonable use without harming other users. Cont...