From b2a67d4a1164ae34f2bd6e8537ab166d918d65a8 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Tue, 14 Oct 2025 20:24:18 +0100 Subject: [PATCH 1/8] refactor code for calculateSum and product --- .../calculateSumAndProduct.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..6d49388 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,24 +9,24 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity:O(2n) originall have this as using 2 separate loops + * Space Complexity:O(1) no extra space used that rows with input + * Optimal Time Complexity:O(n) must at least visit each number once * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ + +// here we are using 2 loops one for sum and other for product +// but we can do it only in one loop so code will be more simple and faster + export function calculateSumAndProduct(numbers) { let sum = 0; - for (const num of numbers) { - sum += num; - } - let product = 1; for (const num of numbers) { + sum += num; product *= num; } - return { sum: sum, product: product, From 4aa063ae3eb480fd48c406f4e024c97c7da3dbf5 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Tue, 14 Oct 2025 20:24:57 +0100 Subject: [PATCH 2/8] refactor code for find commonItems --- .../findCommonItems/findCommonItems.js | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..c07b89b 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,25 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity:O(n+m) it build set and loop through arrays once + * Space Complexity: store second array in set + * Optimal Time Complexity:O(m+n) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +// export const findCommonItems = (firstArray, secondArray) => [ +// ...new Set(firstArray.filter((item) => secondArray.includes(item))), +// ]; +// Refactored to use a Set for faster lookups, making the code more efficient +export const findCommonItems = (firstArray, secondArray) => { + const secondArraySet = new Set(secondArray); + const resultSet = new Set(); + for (const element of firstArray) { + if (secondArraySet.has(element)) { + resultSet.add(element); + } + } + return [...resultSet]; +}; From 5212626a2bc7990eb7d5636633405635d9b6472f Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Tue, 14 Oct 2025 20:25:23 +0100 Subject: [PATCH 3/8] refactor code for hasPairWithSum --- .../hasPairWithSum/hasPairWithSum.js | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..9631c57 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,35 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: o(n2) the function use 2 nested loop + * Space Complexity:o(1)no additional significant memory used + * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find * @returns {boolean} True if pair exists, false otherwise */ +// export function hasPairWithSum(numbers, target) { +// for (let i = 0; i < numbers.length; i++) { +// for (let j = i + 1; j < numbers.length; j++) { +// if (numbers[i] + numbers[j] === target) { +// return true; +// } +// } +// } +// return false; +// } + export function hasPairWithSum(numbers, target) { - for (let i = 0; i < numbers.length; i++) { - for (let j = i + 1; j < numbers.length; j++) { - if (numbers[i] + numbers[j] === target) { - return true; - } + const numbersNeeded = new Set(); // stores numbers we've already seen + + for (const num of numbers) { + const requiredNumber = target - num; + if (numbersNeeded.has(requiredNumber)) { + return true; // found a pair! } + numbersNeeded.add(num); // remember current number } - return false; + + return false; // no pair found } From aad8ad2778f109f6505668310b68290d254b651d Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Tue, 14 Oct 2025 20:25:42 +0100 Subject: [PATCH 4/8] refactor code for remove duplicate --- .../remove_duplicates/remove_duplicates.py | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..b5a5f08 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -3,23 +3,32 @@ ItemType = TypeVar("ItemType") -def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: - """ - Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. +# def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: +# """ +# Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. - Time complexity: - Space complexity: - Optimal time complexity: - """ - unique_items = [] +# Time complexity:O(n²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times. +# Space complexity:O(n) store n elements in worst possible case +# Optimal time complexity: O(n) can be improved useing set +# """ +# unique_items = [] - for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: - unique_items.append(value) +# for value in values: +# is_duplicate = False +# for existing in unique_items: +# if value == existing: +# is_duplicate = True +# break +# if not is_duplicate: +# unique_items.append(value) + +# return unique_items - return unique_items +def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: + unique_element= set() # for unique element + order_element =[] # to order maintain + for value in values: + if value not in unique_element: + unique_element.add(value) + order_element.append(value) + return order_element \ No newline at end of file From 63afa399cdd12715356bfcf2d2994a1ffee39165 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 16 Oct 2025 12:27:59 +0100 Subject: [PATCH 5/8] code update and add cache --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc667..912d94b 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,11 @@ +cache = {} + def fibonacci(n): + if(n) in cache: + return cache[n] if n <= 1: return n - return fibonacci(n - 1) + fibonacci(n - 2) + + result = fibonacci(n - 1) + fibonacci(n - 2) + cache[n] = result + return result From 799f80925a8ade2f24ec2c1ab3c7080b656698f9 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 16 Oct 2025 12:28:46 +0100 Subject: [PATCH 6/8] add cache to speed up computation --- .../making_change/making_change.py | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Sprint-2/improve_with_caches/making_change/making_change.py b/Sprint-2/improve_with_caches/making_change/making_change.py index 255612e..1670605 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,4 +1,4 @@ -from typing import List +from typing import Dict, List, Tuple def ways_to_make_change(total: int) -> int: @@ -7,26 +7,34 @@ def ways_to_make_change(total: int) -> int: For instance, there are two ways to make a value of 3: with 3x 1 coins, or with 1x 1 coin and 1x 2 coin. """ - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) + cache: Dict[Tuple[int, int], int] = {} # Initialize cache + return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1], cache, 0) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: +def ways_to_make_change_helper(total: int, coins: List[int], cache: Dict[Tuple[int, int], int], coin_index: int) -> int: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. """ - if total == 0 or len(coins) == 0: + if total == 0: + return 1 + if total < 0 or coin_index >= len(coins): return 0 + + + # Check cache + key = (total, coin_index) + if key in cache: + return cache[key] ways = 0 - for coin_index in range(len(coins)): - coin = coins[coin_index] - count_of_coin = 1 - while coin * count_of_coin <= total: - total_from_coins = coin * count_of_coin - if total_from_coins == total: - ways += 1 - else: - intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:]) - ways += intermediate - count_of_coin += 1 + coin = coins[coin_index] + count_of_coin = 1 + while coin * count_of_coin <= total: + remaining = total - coin * count_of_coin + ways += ways_to_make_change_helper(remaining, coins, cache, coin_index + 1) + count_of_coin += 1 + + ways += ways_to_make_change_helper(total, coins, cache, coin_index + 1) + + cache[key] = ways # Store result in cache return ways From e1b8e9279793d64349bdf6de716450b460164890 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Fri, 27 Feb 2026 21:36:42 +0000 Subject: [PATCH 7/8] Rename global cache to fibonacci_cache for clarity --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 912d94b..d55d190 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,11 +1,11 @@ -cache = {} +fibonacci_cache = {} def fibonacci(n): - if(n) in cache: - return cache[n] + if(n) in fibonacci_cache: + return fibonacci_cache[n] if n <= 1: return n result = fibonacci(n - 1) + fibonacci(n - 2) - cache[n] = result + fibonacci_cache[n] = result return result From 793a50808e488efdc4c6322c4eedb1494855e5fb Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 28 Feb 2026 20:48:15 +0000 Subject: [PATCH 8/8] revert to start of sprint 2 --- .../calculateSumAndProduct.js | 16 +++---- .../findCommonItems/findCommonItems.js | 23 +++------- .../hasPairWithSum/hasPairWithSum.js | 32 ++++---------- .../remove_duplicates/remove_duplicates.py | 43 ++++++++----------- 4 files changed, 40 insertions(+), 74 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 6d49388..ce738c3 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,24 +9,24 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity:O(2n) originall have this as using 2 separate loops - * Space Complexity:O(1) no extra space used that rows with input - * Optimal Time Complexity:O(n) must at least visit each number once + * Time Complexity: + * Space Complexity: + * Optimal Time Complexity: * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ - -// here we are using 2 loops one for sum and other for product -// but we can do it only in one loop so code will be more simple and faster - export function calculateSumAndProduct(numbers) { let sum = 0; - let product = 1; for (const num of numbers) { sum += num; + } + + let product = 1; + for (const num of numbers) { product *= num; } + return { sum: sum, product: product, diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index c07b89b..5619ae5 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,25 +1,14 @@ /** * Finds common items between two arrays. * - * Time Complexity:O(n+m) it build set and loop through arrays once - * Space Complexity: store second array in set - * Optimal Time Complexity:O(m+n) + * Time Complexity: + * Space Complexity: + * Optimal Time Complexity: * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -// export const findCommonItems = (firstArray, secondArray) => [ -// ...new Set(firstArray.filter((item) => secondArray.includes(item))), -// ]; -// Refactored to use a Set for faster lookups, making the code more efficient -export const findCommonItems = (firstArray, secondArray) => { - const secondArraySet = new Set(secondArray); - const resultSet = new Set(); - for (const element of firstArray) { - if (secondArraySet.has(element)) { - resultSet.add(element); - } - } - return [...resultSet]; -}; +export const findCommonItems = (firstArray, secondArray) => [ + ...new Set(firstArray.filter((item) => secondArray.includes(item))), +]; diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 9631c57..dd2901f 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,35 +1,21 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: o(n2) the function use 2 nested loop - * Space Complexity:o(1)no additional significant memory used - * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups + * Time Complexity: + * Space Complexity: + * Optimal Time Complexity: * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find * @returns {boolean} True if pair exists, false otherwise */ -// export function hasPairWithSum(numbers, target) { -// for (let i = 0; i < numbers.length; i++) { -// for (let j = i + 1; j < numbers.length; j++) { -// if (numbers[i] + numbers[j] === target) { -// return true; -// } -// } -// } -// return false; -// } - export function hasPairWithSum(numbers, target) { - const numbersNeeded = new Set(); // stores numbers we've already seen - - for (const num of numbers) { - const requiredNumber = target - num; - if (numbersNeeded.has(requiredNumber)) { - return true; // found a pair! + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] + numbers[j] === target) { + return true; + } } - numbersNeeded.add(num); // remember current number } - - return false; // no pair found + return false; } diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index b5a5f08..c9fdbe8 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -3,32 +3,23 @@ ItemType = TypeVar("ItemType") -# def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: -# """ -# Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. - -# Time complexity:O(n²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times. -# Space complexity:O(n) store n elements in worst possible case -# Optimal time complexity: O(n) can be improved useing set -# """ -# unique_items = [] - -# for value in values: -# is_duplicate = False -# for existing in unique_items: -# if value == existing: -# is_duplicate = True -# break -# if not is_duplicate: -# unique_items.append(value) +def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: + """ + Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. -# return unique_items + Time complexity: + Space complexity: + Optimal time complexity: + """ + unique_items = [] -def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: - unique_element= set() # for unique element - order_element =[] # to order maintain for value in values: - if value not in unique_element: - unique_element.add(value) - order_element.append(value) - return order_element \ No newline at end of file + is_duplicate = False + for existing in unique_items: + if value == existing: + is_duplicate = True + break + if not is_duplicate: + unique_items.append(value) + + return unique_items