From 47b40f880598b4642a1676430b60113bff04e1cd Mon Sep 17 00:00:00 2001 From: hugoaguirre Date: Tue, 8 Oct 2024 22:17:42 -0600 Subject: [PATCH] formatting and fix list in load balancers post --- content/posts/fast-slow.md | 6 ++++-- content/posts/load-balancers.md | 20 +++++++++----------- content/posts/two-pointers.md | 8 ++++++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/content/posts/fast-slow.md b/content/posts/fast-slow.md index 85b36a5..701dd0e 100644 --- a/content/posts/fast-slow.md +++ b/content/posts/fast-slow.md @@ -5,6 +5,7 @@ draft = false +++ This is a pattern that uses two pointers approach, however there's a special thing about this pattern: + - One pointer moves faster than the other. The Fast and Slow pattern follows the [Hare and Tortoise algorithm](https://en.wikipedia.org/wiki/Cycle_detection). @@ -37,6 +38,7 @@ def hasCycle(head: Node) -> bool: return False ``` +
Also the algorithm helps us to find the middle Node of a linked list in `O(N)` time complexity and time `O(1)` constant space @@ -61,6 +63,7 @@ def findMiddle(head: Node) -> Node: return slow ``` +
So, with this said, we can calculate the node that starts the cycle in a linked list @@ -121,6 +124,5 @@ def getCycleStart(head: Node, cycle_length: int) -> Node: return slow ``` -
- +
diff --git a/content/posts/load-balancers.md b/content/posts/load-balancers.md index b4a70f3..b5dd909 100644 --- a/content/posts/load-balancers.md +++ b/content/posts/load-balancers.md @@ -14,17 +14,15 @@ Load Balancers (LB) are a crucial component for System Design. They are basicall ## How does a request get processed by a Load Balancer? -- Request is received -- Request then is evaluated: - - LB determines which server or resource should handle the request - - At this point, the Load Balancer Algorithm takes into account the following things: - - Server capacity and response time - - Number of active connections - - Geographic location - -- LB forwards request to the server -- Server responds back to the LB -- LB processes response and sends it back to the user/client +1. Request is received +2. Request then is evaluated: + LB determines which server or resource should handle the request using a balancing algorithm taking in consideration the following things: + - Server capacity and response time + - Number of active connections + - Geographic location +3. LB forwards request to the server +4. Server responds back to the LB +5. LB processes response and sends it back to the user/client ## Load Balancer Algorithms diff --git a/content/posts/two-pointers.md b/content/posts/two-pointers.md index 50b8d91..1ce73ab 100644 --- a/content/posts/two-pointers.md +++ b/content/posts/two-pointers.md @@ -9,10 +9,12 @@ Two Pointer is basically what the name says. You've got to use two variables to This approach excels in the following: ## Data structures + - Arrays (sorted or unsorted) - Linked lists ## Problem types + - Find a set of elements that fulfill certain constraints - Pairs, Triplets, Sub-arrays. @@ -23,15 +25,17 @@ The most common problem types you'll encounter are something like this: > Given an array of unsorted numbers and a target number, find a triplet in the array whose sum is as close to the target number as possible. ## Tips and observations + I think the idea is pretty clear right? Either the array is sorted or not, you could always sort it with a pre-built in function and start manipulating the pointers at your wish. A rule of thumb that I've been telling myself to follow is to **never name those pointers `i` and `j`**. Instead, name them `left` and `right`. Those names are much clear and will simplify things in your head. ## Common code structure -For simplicity sake, I'll write things in Python. +For simplicity sake, I'll write things in Python. ### Pointer at the beginning and end of an array + ```python from typing import List @@ -58,6 +62,7 @@ def bothSides(arr: List[int]): ``` ### One main index and pointers at both ends + ```python from typing import List @@ -106,7 +111,6 @@ def rightPullsLeft(arr: List[int]): right += 1 ``` - Alright, that's it for Two Pointers. Keep grinding!