Daily DSA and System Design Journal - 4

I

I.K

Guest
Hello, I'm continuing my journey of daily learning, focusing on System Design concepts (via the roadmap.sh System Design Roadmap) and then tackling DSA challenges on LeetCode. This is DAY 4!

πŸ— System Design: Availability vs. Consistency​


Today’s System Design concept dives into the classic trade-off in distributed systems: Availability vs. Consistency, closely tied to the CAP Theorem.

βœ… Availability​

  • Refers to the ability of a system to remain operational and provide services, even in the presence of failures.
  • Measured in terms of uptime percentage.
  • A highly available system ensures that users always get a response (no timeouts/errors), though the data might not always be the most recent.

πŸ“ Consistency​

  • Guarantees that all clients see the same data at the same time.
  • Important for maintaining data correctness and integrity.
  • A strongly consistent system ensures that a read always reflects the latest write.

βš–οΈ The CAP Theorem​


In distributed systems, you can only achieve two of the following three guarantees at the same time:

  1. Consistency (C) β†’ Every read reflects the most recent write.
  2. Availability (A) β†’ Every request receives a response (no errors).
  3. Partition Tolerance (P) β†’ The system continues operating even if the network is unreliable.

Since network partitions are unavoidable, every distributed system must tolerate them. That leaves us with two design options:


  • CP (Consistency + Partition Tolerance)
    • System waits for consistency, potentially sacrificing availability (timeouts, errors).
    • Example: Banking systems where accuracy of transactions matters more than availability.

  • AP (Availability + Partition Tolerance)
    • System prioritizes availability, even if it returns stale data.
    • Example: Shopping carts or social feeds where being available is more important than strict consistency.

πŸ’­ My Thoughts​


Understanding availability vs. consistency feels like a foundational milestone in system design. Distributed systems bring massive power and resilience but also force tough choices.

This concept reminds me that β€œperfect systems don’t exist” β€” every architecture has trade-offs. The skill lies in knowing when to sacrifice consistency for uptime, or uptime for correctness, depending on the business case.

πŸ’» DSA Challenge: Container With Most Water​


After digging into Availability vs. Consistency, I moved on to a classic two-pointer problem on LeetCode: 11. Container With Most Water.

⏳ Time spent: ~2+ hours
This one was tricky at first β€” but the two-pointer approach really clicked after testing some edge cases.

πŸ”Ž Understanding the Problem​


You’re given an array of heights. Each element represents a vertical line on the x-axis. You need to choose two lines that, together with the x-axis, form a container that holds the most water.

Constraints:

  • Width is determined by the distance between lines.
  • Height is limited by the shorter of the two lines.

🧩 My Approach: Two-Pointer Technique​

Step 1: Initialization​


Code:
max_area = 0
left = 0
right = len(height) - 1
  • max_area: keeps track of the maximum water area.
  • left: pointer at the start.
  • right: pointer at the end.

Step 2: Main Loop​


Code:
while left < right:
    current_area = (right - left) * min(height[left], height[right])
    max_area = max(max_area, current_area)
  • Calculate area as width Γ— min(height[left], height[right]).
  • Update max_area whenever a bigger area is found.

Step 3: Move Pointers​


Code:
if height[left] < height[right]:
    left += 1
else:
    right -= 1
  • Always move the pointer at the shorter line, since moving the taller one won’t increase area.

Step 4: Return Result​


Code:
return max_area

πŸ‘¨β€πŸ’» Full Code Snippet​


Code:
class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        left, right = 0, len(height) - 1

        while left < right:
            current_area = (right - left) * min(height[left], height[right])
            max_area = max(max_area, current_area)

            if height[left] < height[right]:
                left += 1
            else:
                right -= 1

        return max_area

✨ Key Takeaways​

  • The two-pointer method is optimal here β€” avoids brute force O(nΒ²).
  • Moving the smaller pointer is the key insight.

  • Tested with edge cases:
    • Equal heights at ends.
    • One very tall + many short lines.

  • Got much cleaner once I stopped trying to β€œtrack max height” and just trusted the pointer logic.

πŸ™Œ Final Thoughts​


Day 4 gave me two powerful lessons:

  1. In System Design β†’ trade-offs are unavoidable, especially between consistency and availability.
  2. In DSA β†’ two-pointer techniques can massively reduce complexity when the problem involves β€œextremes” from both ends.

Slow but steady, I’m building confidence one day at a time πŸ’ͺ.

🀝 Let’s Connect!​


If you enjoyed this breakdown, let’s chat! Drop a comment, share with others on the same path, or reach out with feedback. Your support keeps me motivated to keep learning daily πŸš€.

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top