Day 8 of 30-day Coding Sprint Today’s focus was on a classic problem that teaches you why linear time isn't always good enough when dealing with exponents. Today's progress on LeetCode: 50. Pow(x, n) The Simple Recursive Approach: Multiplying x by itself n times. - Complexity: O(n) time. - The Issue: For large values of n, this hits the stack limit or simply takes too long. The Optimal Strategy: Binary Exponentiation (Divide & Conquer) - The Logic: Use the property (x^n) = (x^n/2)^2. By halving n at each recursive step, we drastically reduce the number of multiplications. The Result: O(log n) time. This turns a billion operations into roughly 30. #30DaysOfCode #DSASprint #LeetCode #JavaScript #Recursion #Math #Consistency
Optimizing LeetCode's Pow(x, n) with Binary Exponentiation
More Relevant Posts
-
Day 17 of 30-day Coding Sprint Today I’ve shifted focus to one of the most efficient techniques in a developer's toolkit: The Sliding Window. 3. Longest Substring Without Repeating Characters - The Challenge: Finding the length of the longest substring where every character is unique. A naive O(n^2) approach would be too slow for large strings. - The Strategy: Optimized Sliding Window Used a Frequency Hash Array (represented as a 256-element array) to store the last-seen index of each character. Two pointers, l (left) and r (right), define the current window. - The Optimization: Instead of moving the left pointer l one by one when a duplicate is found, I "jump" 1 directly to hash[s[r]] + 1. This ensures that the window always contains unique characters in the most efficient way possible. Result: A clean O(n) time complexity with a single pass through the string. Note: The sliding window isn't just about moving pointers; it's about maintaining a state (in this case, uniqueness) and shrinking or expanding the boundaries to satisfy that state. Using a hash array to store indices turns an O(n) "shrink" into an O(1) "jump." #30DaysOfCode #DSASprint #LeetCode #JavaScript #SlidingWindow #Optimization #Consistency
To view or add a comment, sign in
-
-
Day 25 of 30-days Coding Sprint Today was about finding Distinct Pairs with Difference K, a problem that forces you to handle duplicates carefully to avoid overcounting. The Goal: Find unique pairs (a, b) such that b - a = k. The keyword here is distinct; if we have multiple [1, 5] pairs, we only count them once. Approach 1: The Hash Map (Frequency Logic) - The Strategy: Count the frequency of every number. - The Check: 1. If k > 0, check if map[element + k] exists. - If k = 0, check if map[element] > 1 (since a number minus itself is 0). Pros: Great for O(N) time, but requires extra space for the map. Approach 2: Two-Pointer (Sorted Space) The Strategy: Sort the array first to use the "Expand/Shrink" logic. The Decision Logic: If diff == k: Found a pair! Increment count and skip duplicates using a while loop to maintain uniqueness. If diff < k: Need a larger difference, so move the right pointer. If diff > k: Difference is too big, move the left pointer. Complexity: O(N log N) for sorting, but O(1) auxiliary space. #30DaysOfCode #DSASprint #LeetCode #JavaScript #TwoPointers #Algorithms #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
📌 #45 DailyLeetCodeDose Today's problem: 141. Linked List Cycle – 🟢 Easy There are two ways to solve such tasks 1. Mark visited nodes, ex. by putting them into hash map 2. More clever way to avoid using extra space – 🦍 Floyd’s Cycle Finding Algorithm The main idea of this algorithm is to use 2 pointers at the same time: slow and fast. Slow goes by 1 step, fast by 2 on each cycle iteration. If two pointers on the same list node, we found a cycle! If you want to read more about it, here is the link: https://lnkd.in/ex25Eyvc Link to the leetcode promlem: https://lnkd.in/eM8KTdy2 #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
The hell of parsers. One day, you think you’ve written a simple unit test. The next day, you realize you’ve opened a crack in the geometry of JavaScript. I was testing what felt like a safe invariant: source code → structure → source code. No transformation. No creativity. Just fidelity. And yet, something vanished. No error. No crash. No warning. Just a quiet absence. That’s when you learn the hard lesson: in JavaScript, structure is not always where you expect it to be. Some constructs don’t live inside what they initialize. They orbit it. Parsers rarely fail loudly. They fail silently and structurally. And once you see it, you can’t unsee it. Welcome to the hell of parsers. #Programming #JavaScript #Parsing #AST #Compilers #SoftwareEngineering #SystemsThinking #DeveloperLife #DeepTech #UnderTheHood
To view or add a comment, sign in
-
-
Do you know about Hell? The hell in coding? It is 'Callback hell.' Callback Hell is not a beginner problem. It’s a bad design problem. You start innocent. One API call. Then another. Then another. Suddenly, your code looks like a staircase designed by chaos. This is callback hell, aka pyramid of doom. Why it’s hell: Your code is unreadable. Error handling is a joke. Debugging feels like archaeology. One small change breaks five things. New devs fear touching your file. If you’re still writing code like this in 2026, that’s on you. The fix is not magic. It’s discipline. Use Promises Use async / await Split logic into small, named functions Handle errors once, not everywhere Stop nesting like it’s 2012 Bad code works. Good code scales. Clean code survives teams, time, and pressure. If your code gives you anxiety, it’s trying to tell you something. #JavaScript #WebDevelopment #CleanCode #AsyncAwait #Programming #MERN #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟭𝟯 𝙃𝙤𝙬 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩 𝙂𝙖𝙧𝙗𝙖𝙜𝙚 𝘾𝙤𝙡𝙡𝙚𝙘𝙩𝙞𝙤𝙣 𝙒𝙤𝙧𝙠𝙨 Your code doesn’t free memory. JavaScript does. But it only removes what is unreachable. 𝙍𝙚𝙖𝙘𝙝𝙖𝙗𝙞𝙡𝙞𝙩𝙮 An object stays in memory if there is a reference path from the roots. Roots: • Global scope • Execution stack • Active closures No path → eligible for garbage collection. 𝙈𝙖𝙧𝙠 & 𝙎𝙬𝙚𝙚𝙥 • Start from the roots and mark all reachable objects • Remove everything unmarked Reachable stays. Unreachable is freed. 𝙂𝙚𝙣𝙚𝙧𝙖𝙩𝙞𝙤𝙣𝙖𝙡 𝘾𝙤𝙡𝙡𝙚𝙘𝙩𝙞𝙤𝙣 Most objects die young. Memory is divided into: • Young generation → short-lived objects, frequent fast cleanup • Old generation → long-lived objects, less frequent cleanup 𝙋𝙧𝙤𝙢𝙤𝙩𝙞𝙤𝙣 Every object starts in the young generation. If it survives multiple GC cycles or memory pressure increases, it gets promoted to the old generation. Survival determines placement. 𝙒𝙝𝙮 𝙈𝙚𝙢𝙤𝙧𝙮 𝙇𝙚𝙖𝙠𝙨 𝙎𝙩𝙞𝙡𝙡 𝙃𝙖𝙥𝙥𝙚𝙣 GC removes only unreachable objects. Leaks occur when references are retained: • Globals • Closures • Timers / event listeners • Growing caches 𝘎𝘊 𝘸𝘰𝘳𝘬𝘴 𝘤𝘰𝘳𝘳𝘦𝘤𝘵𝘭𝘺. 𝘠𝘰𝘶𝘳 𝘳𝘦𝘧𝘦𝘳𝘦𝘯𝘤𝘦𝘴 𝘥𝘦𝘤𝘪𝘥𝘦 𝘸𝘩𝘢𝘵 𝘭𝘪𝘷𝘦𝘴. #JavaScript #WebDevelopment #SoftwareEngineering #FrontendDevelopment #Programming #Coding #Developers #TechCommunity #ComputerScience #PerformanceOptimization #Learning #ContinuousLearning #LearnInPublic
To view or add a comment, sign in
-
-
Hello Connections! It's long time no see you all. From past some days I'm less active here and doing some self study. Today I was thinking about a randome probelm - You have a wire of "x" meters length. How many circles can be formed using this wire with Equal Radius? In this problem you have only asked for posible circles. I solved it by considering the radius is fixed or the number of circles are fixed. After solving this equation using the formula of Circumference of Circle = 2πr. The equetions I got are :- 1. r = x/(2πn); where n is fixed and the number of circles, 2. n = x/(2πr); where r is fixed and given radius of circles. After getting these equetions, I used my coding knowledge to simulate it. In the video i have shown the working of my code. For any suggestion, please let me know in comments. reposetory link:- https://lnkd.in/d8Pb7RWJ #coding #challenge #math #webdevelopement #javascript #problemsolving
To view or add a comment, sign in
-
Shall I say, the new Pomodoro timer is just the Claude usage limit for the current session? I’m still very skeptical of "vibe coding," but nobody knows what the future holds. I don't think devs can afford not to learn it. Even the biggest skeptics should prepare for every scenario—that’s the smartest move, in my opinion. I spent a few hours adding a Vulkan backend alongside the OpenGL one in my hobby Java rendering engine. It worked like a charm; it took me two usage limit cycles. I’m really curious to dive deep into the code to see what it actually generated and if it’s truly reliable. #vibecoding
To view or add a comment, sign in
-
Ever feel like your team’s code is a bit of a tangled mess? That was us, not too long ago. Production issues were popping up more often than we liked, and figuring out what went wrong felt like an archaeological dig. It was slowing everything down, honestly. We knew we needed a change, but where to start? My focus shifted towards instilling more disciplined coding practices. It wasn't about pointing fingers, but about building better habits together. We started talking about object-oriented principles and standard design patterns, really digging into how we structured things. I tried to lead by example, sharing insights and offering guidance on our PHP and Python/Django services. It was a lot of back and forth, explaining the ‘why’ behind each suggestion. And you know what? It’s made a noticeable difference. The number of unexpected bugs has gone down. Things are just… more manageable now. It feels good to know the codebase is more robust and that we’re all better equipped to tackle those tricky incidents. It’s a relief, frankly. What have you found most effective in improving code quality within your teams? Would love to hear your thoughts. #SoftwareDevelopment #EngineeringCulture #TechLeadership #CodingPractices #Teamwork
To view or add a comment, sign in
-
🚀 Day 915 of #1000DaysOfCode ✨ Shortest Path Algorithm — Explained Mathematically We often learn shortest path algorithms from a coding perspective. But what if we step back and understand them from a mathematical point of view? In today’s post, I’ve explained the shortest path algorithm in a slightly different way — through mathematical reasoning and structure. It’s a deeper, more conceptual approach that helps you truly understand *why* the algorithm works, not just how to implement it. If you enjoy digging beneath the surface and building strong theoretical foundations behind practical coding problems, this one is for you. 👇 Do you prefer understanding algorithms conceptually first, or jumping straight into code? #Day915 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #Next #CodingCommunity #Algorithms #DataStructures #SystemDesign #ProblemSolving
To view or add a comment, sign in
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