💡 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
Two Sum Algorithm: Timing is Everything
More Relevant Posts
-
🚀 Solved: Valid Parentheses (Stack Based Problem) Today I worked on a classic problem that looks simple but tests your core logic 👇 🧠 Problem: Given a string containing only (), {}, [] 👉 Check if the parentheses are valid 💡 Approach I used: Used a stack Push opening brackets For closing brackets: Check top of stack If match → pop Else → invalid ⚙️ Key Insight: 👉 Order matters more than count 👉 Stack helps track the sequence correctly 🔥 What I learned: Importance of stack in real problems Handling edge cases (empty stack, mismatch) Writing clean conditional logic 📌 Time Complexity: O(n) 📌 Space Complexity: O(n) 💬 Have you solved this problem differently? Would love to know your approach! #javascript #cpp #datastructures #algorithms #coding #leetcode #interviewprep
To view or add a comment, sign in
-
-
Day 01: Precision Matters 🔢 Kicking off my coding journey by tackling a classic: The Plus Minus Challenge. The Problem: Given an array of integers, calculate the ratios of positive numbers, negative numbers, and zeros. The catch? The output must be precise to six decimal places. The Solution: It’s all about iteration and formatting. By looping through the array and categorizing each element, we can determine the counts. The key to meeting the precision requirement in JavaScript is using the .toFixed(6) method. javascript function plusMinus(arr) { let n = arr.length; let positive = 0, negative = 0, zero = 0; for(let val of arr) { if(val > 0) positive++; else if(val < 0) negative++; else zero++; } console.log((positive/n).toFixed(6)); console.log((negative/n).toFixed(6)); console.log((zero/n).toFixed(6)); } plusMinus(arr); Key Takeaway: Even simple logic requires attention to detail—especially when it comes to floating-point precision! #CodingChallenge #JavaScript #ProblemSolving #100DaysOfCode #DataStructures
To view or add a comment, sign in
-
I've read the definition of closures probably 10 times. "A function bundled with its lexical scope." Made sense on paper. But I never truly got it until I came across the backpack analogy. Here's how it actually works: When a function is returned from a higher order function, it doesn't leave empty handed. It carries a backpack. That backpack is attached via a hidden [[scope]] link and contains persistent memory of the variables from its outer environment, even after that outer function has finished executing. The data lives on. Attached to the function. Quietly. And once I understood that, everything clicked: → once() runs a function exactly one time, then remembers it already ran → memoize() caches previous results so you never compute the same thing twice → iterators maintain position in a sequence across multiple calls → module pattern keeps variables private, exposes only what you choose → async programming lets callbacks remember the context they were created in All of these are closures in action. I've been writing JavaScript for 3 years. Going back to the fundamentals has been humbling and genuinely eye opening. If you've been skimming over closures, I'd recommend diving deep. It changes how you read code. #javascript #webdevelopment #softwareengineering #frontenddevelopment #continuouslearning
To view or add a comment, sign in
-
-
I spent hours stuck on a “simple” problem… sorting an array. No tutorial. No copy-paste. Just confusion, frustration, and trial & error. At first, nothing made sense: * Why isn’t my logic working? * Why do I need multiple loops? * Why does swapping break everything? But instead of escaping the discomfort… I stayed. I wrote it. Broke it. Rewrote it. Questioned everything. And then it clicked. I built my own custom sorting method (Bubble Sort) from scratch in JavaScript — not by memorizing, but by understanding. Biggest lesson from this: 👉 Real growth happens when you **sit with confusion instead of avoiding it 👉 Patience > Talent 👉 Struggle is not a sign of failure — it's the path to clarity Also learned: * Sorting is NOT about picking values, it's about **comparing & swapping repeatedly** * One pass is never enough — algorithms evolve step by step * Understanding beats memorization every single time Sharing my final implementation below👇 Course Instructor: Rohit Negi | Youtube Channel: CoderArmy #JavaScript #codiing #WebDevelopment #LearningInPublic #ProblemSolving #fullstackdevelopment
To view or add a comment, sign in
-
-
Spent three hours debugging why my web scraper was failing randomly... Turns out the target site was loading content with a 2-second delay, but only sometimes. Classic intermittent bug. Fixed it by adding proper wait conditions in Playwright instead of hardcoded sleeps. Now it waits for the actual element to appear, not just a fixed time. The scraper went from 70% success rate to 99.8% overnight. This is why I always tell people: async loading breaks everything if you're not careful. Modern websites are not your friend when building automation. What's the weirdest intermittent bug you've had to track down? --- Want to automate your workflows or build AI-powered systems for your business? DM me — I help teams ship automation that actually works. #BuildInPublic #WebScraping #Playwright #DevLife #Automation #Python #Debugging #SideProject
To view or add a comment, sign in
-
-
Your variable names are lying to you. const fetchWill = () => {} ⚠ -will implies Promise. Function is not async. Sumerish fixes this. ───── Last week I posted about building a new language protocol. This week I shipped the tooling. 🔌 VS Code extension — syntax highlighting, hover tooltips, autocomplete, chain-order linting 📦 eslint-plugin-sumerish — enforces type contracts on your variable names in CI ───── The color system alone is worth installing for: Every suffix group gets a distinct color. Amber = tense. Red = negation. Blue = plural. Green = social. You scan a Sumerish chain and your eye groups the meaning before your brain reads the words. ───── But the part I'm most proud of: You don't need to learn Sumerish to read it. Report-view-pl-q? You just understood that. "Could you please look at the report?" The roots stay English. The hyphens make the structure visible. The suffix chain is self-documenting. Learning curve: nearly zero. The only two suffixes you need to guess: -pl and -q. And even those are guessable. ───── Links in the comments — extension, npm, GitHub, demo. #vscode #eslint #typescript #javascript #buildinpublic #devtools #opensource
To view or add a comment, sign in
-
-
📘 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐌𝐨𝐝𝐮𝐥𝐞 (𝐁𝐚𝐬𝐢𝐜) 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 9: 𝐎𝐛𝐣𝐞𝐜𝐭 1.What is object? 2.Object syntax / How to declare? 3.Object pairs / What is object pairs? 4.What is object Property & What is object value? 5.Object method / What is Object method?? 6.Object type / Object is primitive or non-primitive? 7.Access property? 8.Object Delete property / How to delete a property? 9.Object methods? 10.Property types? 11.What is nested object? 12.Why use nested object? 13.What is array-like object? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between object and array? 2.What is Shallow Copy vs Deep Copy?? 3.What is this keyword? 4.How does the this keyword work inside an object? 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 10: 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 1.What is function? 2.Function syntax / How to declare Function? 3.What is arrow function? 4.Arrow function syntax? 5.Function structure? 6.What is parameter? 7.Why use parameter? 8.What is argument? 9.Why use argument? 10.What is implicit in function? 11.What is explicit in function? 12.If function returns nothing, what is the output? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between function declaration and expression? 2.What is arrow function vs normal function? 3.What is callback function? 4.What is higher-order function? 5.Why is Default Parameter used in functions? #DotNet #AspNetCore #MVC #FullStack #SoftwareEngineering #ProgrammingTips #DeveloperLife #LearnToCode #JavaScript #JS #JavaScriptTips #JSLearning #FrontendDevelopment #WebDevelopment #CodingTips #CodeManagement #DevTools
To view or add a comment, sign in
-
-
📘 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐌𝐨𝐝𝐮𝐥𝐞 (𝐁𝐚𝐬𝐢𝐜) 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 6: 𝐀𝐫𝐫𝐚𝐲 & 𝐋𝐨𝐨𝐩𝐬 🔹 𝐀𝐫𝐫𝐚𝐲 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 1.What is array / collection / list? Why use it? 2.Array syntax / How to declare an array? 3.What is array index? 4.Array index starts from? 5.How to access element? 6.How to set value? 7.Findout Array length / How to find total length? 8.Array methods? 9.What is array method? 10.Why use array method? 🔹 𝐋𝐨𝐨𝐩 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 1.What is loop? 2.Why use loop? 3.Loop structure? 4.Types of loop? 5.Array loop / Array specific loops? 6.Array is immutable or mutabl? 7.What is loop iteration? 8.Loop control statements? 9.What is array of object? 10.Why use array of object? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.What is the main difference between slice() and splice()? 1.What is the difference between map() and forEach()? (Which one should you use when?) 2.What is the difference between filter() and find()? (Which one should you use when?) 3.When to use reduce()? 4.Difference between for...in() and for...of()? #DotNet #AspNetCore #MVC #FullStack #SoftwareEngineering #ProgrammingTips #DeveloperLife #LearnToCode #JavaScript #JS #JavaScriptTips #JSLearning #FrontendDevelopment #WebDevelopment #CodingTips #CodeManagement #DevTools
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗦𝗵𝗮𝗹𝗹𝗼𝘄 𝗖𝗼𝗽𝘆 𝘃𝘀 𝗗𝗲𝗲𝗽 𝗖𝗼𝗽𝘆 📦 If you’ve ever updated state and something weird happened… this might be why 👇 🔹 Shallow Copy → copies only the first level 🔹 Nested objects are still referenced (same memory) Example: ➡️ Using { ...obj } or Object.assign() 💡 Problem: Change a nested value… and you might accidentally mutate the original object 😬 🔹 Deep Copy → copies everything (all levels) 🔹 No shared references 🔹 Safe to modify without side effects Example: ➡️ structuredClone(obj) ➡️ or libraries like lodash ⚠️ The common pitfall: You think you made a copy: ➡️ { ...user } But inside: ➡️ user.address.city is STILL linked So when you update it: ❌ You mutate the original state ❌ React may not re-render correctly ❌ Bugs appear out of nowhere 🚀 Why this matters (especially in React): State should be immutable ➡️ Always create safe copies ➡️ Avoid hidden mutations ➡️ Keep updates predictable 💡 Rule of thumb: 🔹 Flat objects? → shallow copy is fine 🔹 Nested data? → consider deep copy Understanding this difference = fewer bugs + cleaner state management And yes… almost every developer gets burned by this at least once 😄 Sources: - JavaScript Mastery - w3schools.com Follow 👨💻 Enea Zani for more #javascript #reactjs #webdevelopment #frontend #programming #coding #developers #learnjavascript #softwareengineering #100DaysOfCode
To view or add a comment, sign in
-
-
📘 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐌𝐨𝐝𝐮𝐥𝐞 (𝐁𝐚𝐬𝐢𝐜) 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 7: 𝐒𝐭𝐫𝐢𝐧𝐠 1.What is string? 2.How to declare string? 3.String Declare Special way or Special purpose string? 4.String Check type? 5.String length Check? 6.String Access index? 7.What is empty string? 8.When to use empty string? 9.String is immutable or mutable? 10.String methods? 11.String concatenation? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between slice and substring? 2.What is template literal? 3.Why string is immutable? 4.What are the benefits of Template Literals (Backticks ``)? 5.Why are strings immutable in JavaScript? How does it work in memory? 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 8: 𝐍𝐮𝐦𝐛𝐞𝐫 & 𝐃𝐚𝐭𝐞 1.What is number? 2.Number Types? 3.Math methods? 4.How to find date? 5.Date methods? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between Math.floor and Math.round? 2.How to generate random number between range? #DotNet #AspNetCore #MVC #FullStack #SoftwareEngineering #ProgrammingTips #DeveloperLife #LearnToCode #JavaScript #JS #JavaScriptTips #JSLearning #FrontendDevelopment #WebDevelopment #CodingTips #CodeManagement #DevTools
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