🗄️ dataclass + Protocol Dict-based code scales until the first refactor. Passing dicts feels fast. Flexible. Obvious. Then the system grows. Key mismatches appear at runtime. Contracts live in Slack threads. Refactors feel risky. The fix is explicit contracts. Use dataclass for shape. frozen=True makes it immutable. Safer for shared state. Use Protocol for boundaries. Any class with a matching get() method satisfies the contract. No inheritance required. Refactors become predictable. Testing becomes simpler. Type checkers catch mismatches early.🔵 ⚪ #Python #SoftwareArchitecture #BackendDevelopment
Alan Viana’s Post
More Relevant Posts
-
𝗘𝘃𝗲𝗿 𝗻𝗼𝘁𝗶𝗰𝗲𝗱 𝘀𝗼𝗺𝗲 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻𝘀 𝗼𝗻 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝘁𝗵𝗮𝘁 “𝗯𝗲𝗮𝘁 𝟭𝟬𝟬%”? __import__("atexit").register( lambda: open("display_runtime.txt", "w").write("0") ) Looks like a hacker trick. Feels smart. 𝗪𝗵𝗮𝘁 𝘁𝗵𝗶𝘀 𝗰𝗼𝗱𝗲 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗱𝗼𝗲𝘀 (𝗹𝗶𝗻𝗲 𝗯𝘆 𝗹𝗶𝗻𝗲): • 𝗮𝘁𝗲𝘅𝗶𝘁 → runs code when the program is about to exit • 𝗿𝗲𝗴𝗶𝘀𝘁𝗲𝗿(...) → hooks a function to run at the very end • 𝗼𝗽𝗲𝗻(...).𝘄𝗿𝗶𝘁𝗲("𝟬") → overwrites the file LeetCode reads to show runtime So when execution finishes, the runtime file gets replaced with 0. The algorithm didn’t get faster. Only the displayed number did. 𝗟𝗲𝘀𝘀𝗼𝗻: If you don’t understand how a metric is calculated, you’ll optimize the metric — not the system. #moxcode #Python #LeetCode #Engineering #SystemsThinking #LearningInPublic
To view or add a comment, sign in
-
-
Python with DSA — Day 33 What I worked on: Prime checks: moved from naive divisibility to the √n optimization and the 6k±1 rule to cut unnecessary iterations. Time complexity intuition: compared O(n) vs O(√n) for primality tests; saw how early exits change best/worst cases. Clean loops & edge cases: handled n <= 1, negative inputs, and printed primes from 1–100 with a tight loop. Key snippet (conceptual): Idea: Only test divisors up to √n; if none divide, the number is prime. #Day33 #PythonWithDSA #DataStructures #DSAJourney #ProblemSolving #PythonProgramming #SoftwareEngineer
To view or add a comment, sign in
-
-
Yesterday I tried to write a simple rate limiter by myself. I searched for some algorithms and decided to try the token bucket. The token bucket is easy. Here's the following things I need to do. - Keep track of the clients requests by IP. - Each IP has tokens. - Remove token per request. - Refill tokens by time. - Verify rate limit by checking available tokens. Although this solution is simple, it has some cons: - Strictly enforces rate limits, which can affect applications needing occasional bursts. - Choosing optimal bucket size and refill rate can be complex. - Requires additional memory and computational overhead to manage tokens. #Python #Coding #RateLimiter #SoftwareEngineering #LearningByDoing
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge – Day 9 Problem #865: Smallest Subtree with All the Deepest Nodes (Medium) Another solid tree + DFS problem that really tests how well you understand recursion and depth tracking. 🧭 How I Approached the Question: The key was realizing that I don’t just need the depth of each subtree — I also need to know which node represents the smallest subtree containing all deepest nodes. So instead of a normal DFS, I designed a recursive function that returns two things: the height of the subtree the candidate node that could be the answer 🧠 Core Insight: For each node: Recursively compute (height, node) from the left and right subtrees Compare their heights: If left height > right height → deepest nodes are on the left If right height > left height → deepest nodes are on the right If equal → current node becomes the smallest subtree containing all deepest nodes This bottom-up approach ensures that: ✔️ Depth is calculated correctly ✔️ The smallest valid subtree is chosen automatically 🛠️ Why This Works: The deepest nodes define the maximum depth. The lowest common ancestor of all deepest nodes is exactly what the problem asks for. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) This problem was a great reminder that: 👉 Sometimes returning more information from recursion makes the solution much cleaner. 📌 Tree problems get easier once you stop thinking top-down and start thinking bottom-up. #LeetCode #DailyCoding #BinaryTree #DFS #Recursion #ProblemSolving #DSA #Python #CodingJourney
To view or add a comment, sign in
-
-
🚀 #DAY74 LeetCode Problem #1200 – Minimum Absolute Difference 🧩 Problem Summary Given a list of distinct integers, the goal is to find all element pairs whose absolute difference is the minimum possible and return them in sorted order. 🧠 How It Works First, sort the array to bring close values together Scan once to compute the minimum difference between adjacent elements Scan again to collect all pairs that match this minimum difference This approach leverages the fact that in a sorted array, the smallest difference must occur between neighboring elements. 📘 What I Learned ✔ Why sorting simplifies difference-based problems ✔ How breaking the task into two passes improves clarity ✔ Turning mathematical observations into efficient logic ✔ Writing clean and readable solutions even when performance isn’t maxed 📊 Performance ⏱ Runtime: 95 ms 💾 Memory: 21.57 MB ✅ 38 / 38 testcases passed 📌 Small optimizations, big clarity — consistency is the real win. #LeetCode #DSA #Algorithms #ProblemSolving #Python #CodingPractice #DataStructures #TechJourney #100DaysOfDSA #LearningByDoing #Consistency #InterviewPrep #CompetitiveProgramming
To view or add a comment, sign in
-
-
🚀 𝟲𝟬 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗖𝗼𝗱𝗶𝗻𝗴 | 𝗗𝗦𝗔 𝘅 𝗥𝗲𝗮𝗹 𝗪𝗼𝗿𝗹𝗱 𝗣𝗿𝗼𝗷𝗲𝗰𝘁𝘀 #Day32 | 𝗦𝗵𝗼𝗿𝘁𝗲𝘀𝘁 𝗖𝘂𝘀𝘁𝗼𝗺𝗲𝗿 𝗦𝘂𝗽𝗽𝗼𝗿𝘁 𝗣𝗮𝘁𝗵 Built a Shortest Customer Support Path system using Breadth-First Search (BFS) to determine the minimal escalation route in a support network. Focused on: • BFS for shortest path • Graph traversal • Queue-based level exploration • Understanding real-world routing and escalation systems 📌 𝗖𝗼𝗱𝗲 𝗮𝗻𝗱 𝗱𝗼𝗰𝘂𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻: https://lnkd.in/gxzGJ4nB Feedback and suggestions are welcome. #DSA #Graphs #BFS #ShortestPath #Python #60DaysOfCoding #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
Recently solved "LeetCode 1970 – Last Day Where You Can Still Cross" and learned an interesting optimization. My first approach used DFS + binary search, which worked but wasn’t very efficient. Instead, I tried a reverse incremental BFS: - Start with all cells flooded - Unflood cells one by one in reverse order - Incrementally propagate reachability from the top row Each cell is processed at most once, giving amortized O(R×C) runtime — similar to Union-Find, but using BFS. Even if you’re not familiar with DSU, this method is intuitive: just track which cells are reachable from the top as you unflood the grid. While most online solutions use Union-Find or binary search + BFS/DFS, this incremental BFS approach is less commonly discussed and performs extremely well (~8× faster than DFS + binary search in Python). Sharing the solution here: 🔗 [https://lnkd.in/gVkzaSgj] #algorithms #problemSolving #softwareengineering
To view or add a comment, sign in
-
Asyncio feels "safe" until one blocking call freezes everything. 🥶 One stray time.sleep(), a sync client, or a CPU-heavy loop, and suddenly all your concurrent tasks stall at once. Requests slow down, background jobs drift, and nothing in the logs looks obviously wrong. Mehdi Abaakouk wrote a post showing a very simple trick to catch this early in production: measure event loop latency with a tiny watchdog coroutine and turn it into a real signal. If you're running Python async code in prod, this is one guardrail worth having. Link in comment!
To view or add a comment, sign in
-
-
Solving the Bitwise OR Challenge 🔢 The problem: Find the smallest 'x' such that x OR (x + 1) equals a given prime number 'p'. The Logic: 💡 The only prime number that cannot satisfy this is 2. For all other primes (which are odd), the binary representation ends in at least one '1'. 💡 The operation x OR (x + 1) essentially fills the rightmost '0' bit of 'x' with a '1'. 💡 To minimize 'x' while satisfying the condition, we look for the rightmost sequence of consecutive 1s in the prime 'p'. 💡 By flipping the highest bit of that specific trailing sequence from 1 to 0, we create our answer. Example Walkthrough: 👉 For p = 13 (1101): The trailing 1 is at position 0. Flipping it gives 1100 (12). 12 OR 13 = 13. 👉 For p = 31 (11111): The sequence of 1s is long. Flipping the bit at the "boundary" gives 01111 (15). 15 OR 16 = 31. Code Implementation: https://htmlify.me/r/u0q7 Key Takeaway: When dealing with x OR (x + 1), you are looking at how a carry bit propagates through trailing ones in binary addition! #BitManipulation #Programming #Algorithms #Python #CompetitiveProgramming #TechTips
To view or add a comment, sign in
-
-
🗓 Day 66 / 100 – #100DaysOfLeetCode 📌 Problem 865: Smallest Subtree with All the Deepest Nodes Today’s problem focused on binary tree depth analysis. The goal was to find the smallest subtree that contains all the deepest nodes in the tree. 🧠 My Approach: Used Depth-First Search (DFS) to compute information bottom-up. For each node, tracked: the maximum depth reachable from that node, and the candidate subtree root that contains all deepest nodes. Compared depths of left and right subtrees: If both sides have the same depth → the current node is the answer. Otherwise, propagated the deeper side upward. Returned the node that satisfies the condition for all deepest nodes. This approach cleanly combines depth calculation with subtree selection in a single traversal. 💡 Key Learning: This problem reinforced: ✔ how returning multiple values from DFS simplifies logic ✔ thinking bottom-up for tree optimization problems ✔ identifying the exact node where depths converge Tree problems often become elegant once the right recursive structure is identified 🌳🚀 #100DaysOfLeetCode #LeetCodeChallenge #Python #ProblemSolving #BinaryTree #DFS #TreeTraversal #Algorithms #DSA #CompetitiveProgramming #SoftwareEngineering #CodingJourney #DeveloperJourney #LearningInPublic #TechStudent #CareerGrowth #Programming #KeepLearning
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development