Skip to content

Glasgow | 26- SDC-Mar | Taras Mykytiuk | Sprint 1 | Analyse and Refactor Functions#166

Open
TarasMykytiuk wants to merge 3 commits into
CodeYourFuture:mainfrom
TarasMykytiuk:sprint-1
Open

Glasgow | 26- SDC-Mar | Taras Mykytiuk | Sprint 1 | Analyse and Refactor Functions#166
TarasMykytiuk wants to merge 3 commits into
CodeYourFuture:mainfrom
TarasMykytiuk:sprint-1

Conversation

@TarasMykytiuk

@TarasMykytiuk TarasMykytiuk commented Jun 12, 2026

Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Exercises for js and python are done.

@TarasMykytiuk TarasMykytiuk added 📅 Sprint 1 Assigned during Sprint 1 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Jun 12, 2026
Comment on lines +12 to +14
* 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)

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.

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.

* 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

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()?

Comment on lines +13 to +15
inputSequence.forEach((element) => {
if (!uniqueItems.includes(element)) {
uniqueItems.push(element);

@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 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.

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?

Comment on lines +17 to 18
if value not in unique_items:
unique_items.append(value)

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).

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Complexity The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants