Posts

Showing posts from July, 2025

Day 15: Valid Anagram – JavaScript DSA Problem with Explanation

๐Ÿš€ Day 15 – JavaScript DSA Challenge: Valid Anagram ๐Ÿ“Œ Problem: Given two strings s and t , determine if t is an anagram of s . ๐Ÿงช Examples: Input: "anagram", "nagaram" ➡️ true Input: "rat", "car" ➡️ false ✅ Solution 1: Sort and Compare function isAnagram(s, t) { return s.split('').sort().join('') === t.split('').sort().join(''); } ✅ Solution 2: Frequency Map function isAnagramFreq(s, t) { if (s.length !== t.length) return false; const count = {}; for (let char of s) { count[char] = (count[char] || 0) + 1; } for (let char of t) { if (!count[char]) return false; count[char]--; } return true; } ๐ŸŽฏ Tips: Sorting works but is slower Hash map gives best performance for large strings ๐Ÿ”— Follow Me: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 14: First Non-Repeating Character – JavaScript DSA

๐Ÿš€ Day 14 – First Non-Repeating Character in JavaScript This string problem is frequently asked in product companies like Amazon and PayPal. ๐Ÿงช Problem: Given a string, return the index of the first non-repeating character. If no such character exists, return -1. ๐Ÿ“Œ Examples: Input: "leetcode" ➡️ Output: 0 Input: "loveleetcode" ➡️ Output: 2 Input: "aabbcc" ➡️ Output: -1 ✅ JavaScript Code: function firstUniqueChar(str) { const freq = {}; for (let char of str) { freq[char] = (freq[char] || 0) + 1; } for (let i = 0; i ✨ Alternate Solution (indexOf + lastIndexOf) function firstUniqCharBuiltIn(s) { for (let i = 0; i ๐ŸŽฏ Tips: Use a hash map to store character frequency indexOf + lastIndexOf is slower but useful for short strings ๐Ÿ”— Follow for more: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 13: Find Missing Number in an Array – JavaScript DSA Challenge

๐Ÿ”” Day 13: Find Missing Number in an Array – JavaScript DSA Welcome to #Day13 of the CodeWithJaffer DSA series! ๐Ÿ“Œ Problem: Given an array containing numbers from 0 to n with one number missing, find that missing number. ๐Ÿงช Examples: Input: [3, 0, 1] Output: 2 Input: [0, 1, 2, 4, 5] Output: 3 ✅ JavaScript Code: function findMissingNumber(arr) { const n = arr.length; const total = (n * (n + 1)) / 2; const sum = arr.reduce((acc, curr) => acc + curr, 0); return total - sum; } console.log(findMissingNumber([3, 0, 1])); // 2 console.log(findMissingNumber([0, 1, 2, 4, 5])); // 3 ๐Ÿ’ก Tips: Use math formula for sum: n(n+1)/2 Reduce method calculates actual sum Subtract to get missing number ๐ŸŽฏ Bonus: Try solving with XOR approach too – it’s commonly asked in interviews! ๐Ÿ”— Follow me for more: LinkedIn: www.linkedin.com/in/reach-jaffer YouTube: @codewithjaffer

Day 12: First Non-Repeating Character in a String – JavaScript DSA Challenge

๐Ÿ”” Day 12: First Non-Repeating Character in a String – JavaScript DSA Welcome to #Day12 of our CodeWithJaffer JavaScript DSA Challenge! In this post, we will find the first non-repeating character in a given string using simple JavaScript logic. ๐Ÿ“Œ Problem Statement: Given a string, find the first character that does not repeat. If all characters repeat, return -1 . ๐Ÿงช Example: Input: "javascript" Output: "j" Input: "aabbcc" Output: -1 ✅ JavaScript Code: function firstUniqueChar(str) { let freq = {}; for (let char of str) { freq[char] = (freq[char] || 0) + 1; } for (let char of str) { if (freq[char] === 1) return char; } return -1; } // Output Examples console.log(firstUniqueChar("javascript")); // "j" console.log(firstUniqueChar("aabbcc")); // -1 ๐Ÿ’ก Tips to Remember: Use a for...of loop to count each character

Day 10: Reverse Words in a Sentence | JavaScript DSA Challenge with Output & Tips

Day 10: Reverse Words in a Sentence – JavaScript DSA Welcome to Day 10 of our #CodeWithJaffer JavaScript DSA challenge! Today’s task is to reverse every word in a sentence but preserve the word order. ๐Ÿงช Example: Input: "Code With Jaffer" Output: "edoC htiW reffaJ" ✅ JavaScript Code (2 Methods): // One-liner using map function reverseWords(str) { return str.split(" ").map(word => word.split("").reverse().join("")).join(" "); } // For-loop approach function reverseWordsLoop(str) { let words = str.split(" "); let result = []; for (let word of words) { let reversed = word.split("").reverse().join(""); result.push(reversed); } return result.join(" "); } ๐Ÿง  Output: console.log(reverseWords("Code With Jaffer")); // "edoC htiW reffaJ" ๐Ÿ“Œ Tips: Use split(" ") for splitting sentence into words split("").r...

Day 9 – Count Vowels in a String using JavaScript | DSA Challenge with Code & Explanation

  ๐Ÿง  Day 9: Count Vowels in a String – JavaScript Interview Prep Welcome to Day 9 of the #CodeWithJaffer JavaScript DSA series! Today, we’ll solve a popular string-based interview problem: “Count the number of vowels in a given string.” ๐Ÿ” Problem Statement: Write a JavaScript function that takes a string as input and returns the count of vowels (a, e, i, o, u) present in it. ✅ Example Input & Output: // Input: "CodeWithJaffer" // Output: 5 vowels (o, e, i, a, e) ๐Ÿ’ก Method 1: Using RegEx function countVowels(str) { return (str.match(/[aeiou]/gi) || []).length; } ✅ Explanation: /[aeiou]/gi matches all vowels (both uppercase and lowercase). match() returns an array of matches, or null if none. || [] ensures no error when match returns null. .length gives the count of vowels. ๐Ÿ’ก Method 2: Using For-Loop function countVowelsLoop(str) { let count = 0; const vowels = "aeiouAEIOU"; for (let char of str) { if (vowels.i...

Day 8: Find Duplicate Number in Array – JavaScript Interview Question

 ๐Ÿ“Œ Problem: > Given an array of integers, find any duplicate number. ๐Ÿงช Example: Input: [1, 2, 3, 4, 2] Output: 2 --- ✅ Solution 1 – Using Set: function findDuplicate(arr) {   const seen = new Set();   for (let num of arr) {     if (seen.has(num)) return num;     seen.add(num);   }   return -1; } --- ✅ Solution 2 – Sort & Compare: function findDuplicate(arr) {   arr.sort();   for (let i = 1; i < arr.length; i++) {     if (arr[i] === arr[i - 1]) return arr[i];   }   return -1; } --- ๐Ÿ’ก Tips to Remember: Set is great for uniqueness checks Sorting reduces space, but increases time: O(n log n) Use return -1 for "no duplicate" fallback

JavaScript DSA – Find the Missing Number in an Array

 ๐Ÿ” Problem Statement: > You are given an array containing n distinct numbers taken from 1 to n+1. One number is missing. Find it efficiently using JS. ๐Ÿ’ก Solution: function findMissingNumber(arr) {   const n = arr.length + 1;   const expectedSum = (n * (n + 1)) / 2;   const actualSum = arr.reduce((acc, num) => acc + num, 0);   return expectedSum - actualSum; } console.log(findMissingNumber([1, 2, 4, 5, 6])); // Output: 3 ๐Ÿง  Explanation: We know the sum of 1 to n is n*(n+1)/2. Subtracting the actual sum from this gives the missing number. This approach is better than nested loops or sorting. --- ✅ Bonus Tip: > Use .reduce() in JS for summing values in arrays. It’s a commonly asked method in interviews! --- ✅ Try It Yourself: What if 2 numbers are missing? How to solve it without extra space? --- ๐Ÿ“ข Engage: Share your variation in the comments. Follow the Code With Jaffer Blog for daily tips!

Day 6 – Fibonacci Series in JavaScript: Loop vs Recursion

 ๐Ÿ”ฅ JavaScript DSA Interview Series – Day 6 ๐Ÿ’ก Topic: How to generate Fibonacci numbers in JavaScript using both loop and recursion. --- ✅ What is the Fibonacci Series? The Fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Each number is the sum of the previous two: F(n) = F(n - 1) + F(n - 2) ๐Ÿ“Œ Base Conditions: F(0) = 0, F(1) = 1 --- ๐Ÿ” 1. Loop-Based Fibonacci Generator function fibonacciLoop(n) {   let fib = [0, 1];   for (let i = 2; i < n; i++) {     fib[i] = fib[i - 1] + fib[i - 2];   }   return fib.slice(0, n); }

Day 5 – Factorial of a Number in JavaScript: Recursion vs Loop

 ๐Ÿ”ฅ JavaScript DSA Interview Series – Day 5 ๐Ÿง  Problem: Find the factorial of a number using both loop and recursion --- ✅ What is Factorial? The factorial of a number n is the product of all positive integers less than or equal to n. ๐Ÿ“Œ n! = n × (n−1) × (n−2) × ... × 1 Example: 5! = 5 × 4 × 3 × 2 × 1 = 120 --- ๐Ÿ“Œ Iterative (Loop) Approach: function factorialLoop(n) {   let result = 1;   for (let i = 2; i <= n; i++) {     result *= i;   }   return result; } --- ๐Ÿ“Œ Recursive Approach: function factorialRecursive(n) {   if (n === 0 || n === 1) return 1;   return n * factorialRecursive(n - 1); } --- ⚠️ Edge Case: 0! is 1 Negative numbers are not valid for factorial --- ๐Ÿ’ก Tips to Remember: Use recursion only for small inputs (can cause stack overflow) Loop is efficient and safe for interviews Recursive solution shows understanding of function calls and base conditions http://youtube.com/post/Ugkx1JuFK5mB2cSUAX8Yu0N6eFe8RuioUQ3Z?si=vN94FOnh01D...

Day 4 – Palindrome Number in JavaScript Without Converting to String

๐Ÿ”ฅ Interview Series – Day 4 ๐Ÿ’ก Topic: How to check if a number is a palindrome without converting it to a string in JavaScript ✅ Problem Statement Check whether a given number is a palindrome without converting it to a string. A number is a palindrome if it reads the same backward as forward. ๐Ÿงช Examples: 121 → true 123 → false 1221 → true -121 → false (negative numbers are not palindromes) ๐Ÿง  JavaScript Code (Math-Based Logic): function isPalindromeNumber(num) {   if (num < 0) return false;   let original = num;   let reversed = 0;   while (num > 0) {     reversed = reversed * 10 + (num % 10);     num = Math.floor(num / 10);   }   return original === reversed; } ๐Ÿ’ก Tips to Remember: Avoid using .toString() — focus on mathematical logic. % (modulo) gives the last digit. Math.floor() removes the last digit from the number. Store the reversed number and compare it to the original. ๐Ÿง‘‍๐Ÿ’ป Why Interviewers Ask This: To test pure logic-b...