Just solved a classic algorithm problem and recorded my approach. I started with a linear search (O(n)) solution — simple, clear, and a great baseline for understanding the problem. Walking through it step by step really helped reinforce how time complexity grows with input size. Then I explored how to improve it to O(log n) using binary search when the array is sorted — a great reminder of how powerful optimization can be. Key takeaway: Start with a working solution Then optimize for performance Sharing my thought process in this video — would love your feedback and how you usually approach problems like this. #coding #javascript #algorithms #leetcode #softwareengineering #100daysofcode
More Relevant Posts
-
📌 #81 DailyLeetCodeDose Today's problem: 191. Number of 1 Bits – 🟢 Easy The solution looks like magic... and honestly, it kind of is! Meet: 𝐁𝐫𝐢𝐚𝐧 𝐊𝐞𝐫𝐧𝐢𝐠𝐡𝐚𝐧’𝐬 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 👉 n = n & (n - 1) – removes the lowest set bit in one operation It becomes clearer with an example: n = 1011000 n - 1 = 1010111 1011000 & 1010111 --------- 1010000 – last 1 disappeared! Another example: 👉 First iteration: n = 1011 n - 1 = 1010 n & (n - 1) = 1010 count = 1 👉 Second iteration: n = 1010 n - 1 = 1001 n & (n - 1) = 1000 count = 2 👉 Third iteration: n = 1000 n - 1 = 0111 n & (n - 1) = 0000 count = 3 💡 Instead of checking all 32 bits, we only loop through the number of 1s – that's why this trick is both elegant and efficient. https://lnkd.in/euvXvU3X Have you seen this trick before, or was it new to you? :) #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
Solved Leetcode 497 - Next Greater Element I Here’s the key insight: Use a monotonic decreasing stack. Traverse from right to left. For each element, remove all smaller elements from the stack. The top of the stack becomes the next greater element. To make it efficient for queries, store the results in a map: number → next greater element This reduces the complexity from O(n²) to O(n). What clicked for me was this: The stack is used for computation, and the map is used for fast retrieval. Once you understand this separation of roles, a whole category of problems becomes easier: Next Greater Element Previous Greater Element Stock Span Daily Temperatures #DataStructures #Algorithms #JavaScript #CodingInterview #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
Implemented the Bubble Sort algorithm in JavaScript to sort an array in ascending order using a simple comparison and swapping technique. Demonstrates a fundamental sorting approach with O(n²) time complexity, ideal for understanding core algorithm concepts. #JavaScript #DSA #Algorithms #Coding #WebDevelopment
To view or add a comment, sign in
-
-
🚀 Mastering a Classic Algorithm: Balanced Parentheses (Stack – LIFO) Today I revisited a fundamental problem that every Software Engineer should understand: validating balanced parentheses using a stack. Why it matters: This pattern appears in compilers, interpreters, and even real-world applications like expression parsing. Here’s the idea: 👉 Use a stack (LIFO) 👉 Push opening brackets 👉 Pop and match when encountering closing brackets 👉 Ensure the stack is empty at the end Clean and efficient JavaScript implementation 👇 This is a great reminder that mastering data structures like stacks is key to solving real algorithmic problems efficiently. I’m currently building and documenting algorithm patterns here: 🔗 https://lnkd.in/ej4fNeZs #SoftwareEngineering #JavaScript #Algorithms #DataStructures #Coding #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
Day 07: Cracking the "Non-Divisible Subset" Logic 🧩 Today was a true test of algorithmic thinking. I tackled a problem that looks like a standard array search but is actually a brilliant exercise in Number Theory and Remainder Math. The Challenge: Given a set of numbers, find the maximum size of a subset where the sum of any two numbers is not divisible by K The Strategy (Remainder Frequency): Instead of checking every possible pair (which would be very slow), I focused on remainders . If two numbers sum to a multiple of K, their remainders (r1+r2) must sum to K. const s = [19,10,12,10,24,25,22]; const k = 4; function nonDivisibleSubset(k, s) { let freq = new Array(k).fill(0); // count remainders for (let num of s) { freq[num % k]++; } let count = 0; // remainder 0 case if (freq[0] > 0) count++; // check pairs for (let i = 1; i <= Math.floor(k / 2); i++) { if (i === k - i) { // special case when k is even if (freq[i] > 0) count++; } else { count += Math.max(freq[i], freq[k - i]); } } return count; } console.log(nonDivisibleSubset(k,s)) Key Takeaway: When a problem involves divisibility, don't look at the numbers—look at the remainders. It turns a complex pairing problem into a simple counting one! One full week of coding done. The momentum is real! 🚀 #JavaScript #Algorithms #NumberTheory #100DaysOfCode #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Just Uploaded a New LeetCode Solution! Solved LeetCode #26 – Remove Duplicates from Sorted Array using the Two Pointer Technique in JavaScript. This is a must-know pattern for coding interviews — simple idea, but very powerful when applied correctly. 👉 In this video, I’ve covered: Intuition behind the problem Step-by-step dry run Optimal in-place solution (O(n) time, O(1) space) Clean and easy-to-understand JavaScript code 🔗 Watch here: https://lnkd.in/g-HgkGXQ If you're preparing for coding interviews or strengthening your DSA fundamentals, this one is definitely worth your time. Would love to hear your approach to this problem — drop it in the comments 👇 #leetcode #dsa #codinginterview #javascript #twopointer #programming #softwaredevelopment #coding #developers #learning #jdcodebase
Remove Duplicates from Sorted Array | Optimal Two Pointer Approach 🔥 | LeetCode Explained (JS)
https://www.youtube.com/
To view or add a comment, sign in
-
The useMemo and useCallback issue 🧠 Hi there! 👋 Recently, I came across an interesting project 🏗️ in which every component included useCallback and useMemo. However, most of the components had neither hooks nor child elements with memo. 🚫 This was a large-scale project with a complex interface 🧩 that consisted of 4–5 major modules, and some of them had the same issue. 📉 This problem wouldn't be so serious if the product weren't being delivered to users with low-powered PC. 💻🐢 To avoid this kind of situation, I recommend using the React Compiler ⚛️ instead of useMemo, useCallback, memo, or following these guidelines: – Use the useMemo when computations are truly expensive (sorting 1k+ items, regex, tree walks) ⚡ – Use useMemo and useCallback as essential deps (useEffect, props or callbacks in a memo component) 🔗 – Use memo in complex components (large lists, a child sidebar/navbar that doesn't need to be re-rendered) 🖼️ Success with your projects! 🌟 #FrontendDevelopment #ReactJS #WebDevelopment #Optimization #SeniorDeveloper #CodingBestPractices #Developers #Programming #TechTips
To view or add a comment, sign in
-
-
🚀 Week 6 Backend Dev Challenge: Regular Expressions(Regex) This week, I worked on something that felt both nerdy and surprisingly exciting: validating a credit card number using JavaScript. Yes I know, credit card validation doesn’t sound exciting at first… but once you dive into Regular Expressions, your brain suddenly enters “detective mode.” 😅 I had a Verve card lying around so I decided to use it for this. I made some research and found out Verve cards had different prefixes. Some started with 5060, 5078 and 6500. To validate the card, I had to use a regex pattern. Think of regex as that strict friend who checks every tiny detail before letting anyone into the party. 😂 The pattern I used: ^(5060|5078|6500)[0-9]{12,13}$ What it does: ✅️Makes sure the card number starts with a valid Verve prefix ✅️Ensures the rest are numbers only ✅️Checks that the length is just right and blocks anything that looks suspicious 👀 It’s like building a mini-security gate with code. Instead of writing just a random function, I challenged myself to use Object-Oriented Programming which I’ve been learning recently. So I created a class, added properties, and built a validate() method inside it. Suddenly, my little validator felt more like a program and less like a quick hack. The cool part? Using OOP made it super easy to create multiple card objects: card1 → valid card2 → invalid Each one tested itself, like they had their own personalities. OOP really helps you write cleaner, more organized, and more scalable code. I finally get why people hype it so much. 😄 Honestly, this task made me appreciate how much detail goes into something as “simple” as validating a card number. #JavaScript #LearnToCode #Regex #OOP #CodingJourney #BackendDev
To view or add a comment, sign in
-
🚀 Day 24 of My Coding Journey — Power of Two Today’s problem was “Power of Two” — a simple-looking question that really highlights the beauty of bit manipulation. 🔍 What I learned: Instead of using loops or recursion, I explored how binary representation works. A power of two always has only one set bit (1) in its binary form — and that insight leads to a super efficient solution. 💡 Key trick: n & (n - 1) === 0 This removes the lowest set bit, and if the result is zero, the number is a power of two. ⚡ Takeaway: Sometimes the most optimized solutions come from understanding how data is represented internally, not just from writing more code. 📈 Progress: Day by day, I'm getting more comfortable with problem-solving patterns and thinking beyond brute force approaches. #128DaysOfCode #LeetCode #JavaScript #CodingJourney #ProblemSolving #BitManipulation
To view or add a comment, sign in
-
-
💡 Understanding Two Sum — It’s Not About the Numbers, It’s About Timing Today I revisited the classic Two Sum problem and realized something simple but powerful: 👉 The algorithm doesn’t try to find the best pair 👉 It doesn’t look for largest numbers 👉 It doesn’t check all possibilities It simply returns the first valid pair it encounters during traversal Key Insight: The result depends entirely on the order of iteration Example: nums = [1, 2, 4, 5, 6, 8] target = 10 We might expect: 2 + 8 = 10 → indices [1, 5] But the algorithm returns: 4 + 6 = 10 → indices [2, 4] Why? Because: 4 is seen earlier 6 appears soon after The algorithm stops immediately when it finds the first match The Rule to Remember: 🧠 Hash map solution returns the first valid pair based on traversal order Takeaway: This isn’t just about solving Two Sum — it’s about understanding how algorithm behavior is shaped by execution flow, not just logic. Once you see this, you stop memorizing solutions and start actually understanding them. #JavaScript #Algorithms #CodingInterview #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
Explore related topics
- How to Improve Array Iteration Performance in Code
- How to Improve Code Performance
- LeetCode Array Problem Solving Techniques
- Leetcode Problem Solving Strategies
- Approaches to Array Problem Solving for Coding Interviews
- Tips for Finding Simple Solutions to Complex Problems
- Strategies for Solving Algorithmic Problems
- Solving Sorted Array Coding Challenges
- How to Optimize Search Using Embeddings
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