Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: before fix O(2N), after double loop removed - O(N)
* Space Complexity: 0(N) - depends on numbers.length
* Optimal Time Complexity: O(N)
Comment on lines +12 to +14

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • In complexity analysis, O(2N) is the same as O(N).
  • In practice, combining code in two loops into one could help improve the performance, but rarely make the function run twice as fast.
  • In space complexity analysis, the space usually refers to the auxiliary space (extra memory) used by the algorithm in addition to the input itself.

*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
*/
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;
}

Expand Down
15 changes: 9 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: worst O(N1 * N2)
* Space Complexity: worst O(N1 + N2)
* Optimal Time Complexity: worst become: O(N1 + N2)
*
* @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) => {
const firstSet = new Set(firstArray);
const secondSet = new Set(secondArray);
secondArray = [...secondSet].filter((element) => firstSet.has(element))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Most set implementation supports typical set operations like union and intersect. You could also explore using them.

return secondArray;
};
14 changes: 7 additions & 7 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: worst O(N!) - factorial

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you derive the complexity of O(N!)?
N! = N * (N-1) * (N-2) * ... * 2 * 1

* Space Complexity: O(numbers.length)
* Optimal Time Complexity: worth become O(N)
*
* @param {Array<number>} 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;
}
if (numbers.includes(target - numbers[i])) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the complexity of numbers.includes()?

return true;
}
}
return false;
Expand Down
30 changes: 7 additions & 23 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(N*N) quadratic
* Space Complexity: O(N)
* Optimal Time Complexity: become O(N)
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];

for (
let currentIndex = 0;
currentIndex < inputSequence.length;
currentIndex++
) {
let isDuplicate = false;
for (
let compareIndex = 0;
compareIndex < uniqueItems.length;
compareIndex++
) {
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
uniqueItems.push(inputSequence[currentIndex]);
inputSequence.forEach((element) => {
if (!uniqueItems.includes(element)) {
uniqueItems.push(element);
Comment on lines +13 to +15

@cjyuan cjyuan Jun 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complexity of this implementation is still O(N*N). Can you figure out why?

}
}
});

return uniqueItems;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
"sum": 10, // 2 + 3 + 5
"product": 30 // 2 * 3 * 5
}
Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(N*2)
Space Complexity: O(N)
Optimal time complexity: O(N)
"""
# Edge case: empty list
if not input_numbers:
return {"sum": 0, "product": 1}

sum = 0
for current_number in input_numbers:
sum += current_number

product = 1
for current_number in input_numbers:
sum += current_number
product *= current_number


return {"sum": sum, "product": product}
15 changes: 6 additions & 9 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: worst O(N1 * N2)
Space Complexity: worst O(N1 + N2)
Optimal time complexity: worst become: O(N1 + N2)
"""
common_items: List[ItemType] = []
for i in first_sequence:
for j in second_sequence:
if i == j and i not in common_items:
common_items.append(i)
return common_items
firstSet = set(first_sequence)
secondSet = set(first_sequence)
return list(firstSet.intersection(secondSet))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of built-in function.

12 changes: 6 additions & 6 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
"""
Find if there is a pair of numbers that sum to a target value.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: worst O(N!) - factorial
Space Complexity: O(numbers.length)
Optimal time complexity: worth become O(N)
"""

for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == target_sum:
return True
if (numbers[i] - target_sum) in numbers:

@cjyuan cjyuan Jun 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you look up the complexity of in operation on a list (numbers is a list) in Python?

return True
return False
15 changes: 5 additions & 10 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ 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:
Time complexity: O(N*N) quadratic
Space complexity: O(N)
Optimal time complexity: become O(N)
"""
unique_items = []

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:
if value not in unique_items:
unique_items.append(value)
Comment on lines +17 to 18

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unique_items is a list, so the complexity of the in operation on line 18 is not O(1).


return unique_items
Loading