Skip to content

London| 26 March SDC | Jamal Laqdiem | Sprint 1 | Refactor Functions#196

Open
jamallaqdiem wants to merge 3 commits into
CodeYourFuture:mainfrom
jamallaqdiem:refactor-functions
Open

London| 26 March SDC | Jamal Laqdiem | Sprint 1 | Refactor Functions#196
jamallaqdiem wants to merge 3 commits into
CodeYourFuture:mainfrom
jamallaqdiem:refactor-functions

Conversation

@jamallaqdiem

Copy link
Copy Markdown

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

Refactored the scripts js and py to optimise the time and space complexity.

@jamallaqdiem jamallaqdiem added Module-Complexity The name of the module. 📅 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. labels Jun 30, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code is correct.

  • Could you also include the time complexity of the original implementation?

    Note: I think "Time Complexity" in the files refers to the time complexity of the original implementation, and "Optimal Time Complexity" refers to the time complexity of the refactored version.

Comment thread Sprint-1/JavaScript/findCommonItems/findCommonItems.js Outdated
Comment on lines +5 to +6
* Space Complexity:O(n+m)
* Optimal Time Complexity:O(1)

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 explain how you derived the time complexity of $O(1)$ and the space complexity of $O(n+m)$?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for reviewing, is the the Optimal time complexity that should be O(n+m), we have to spend O(m) time to build the set and O(n) time to filter firstArray.
the space Com should be O(u) to store the matching items.

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 $u$? Normally we express all complexity based on the size of the input, which are $n$ and $m$.

Comment thread Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js Outdated
@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 30, 2026
@jamallaqdiem jamallaqdiem added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 1, 2026
Comment thread Sprint-1/Python/find_common_items/find_common_items.py Outdated
@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 1, 2026
@jamallaqdiem jamallaqdiem added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 2, 2026
Comment on lines +12 to +14
Time Complexity: O(n^3)
Space Complexity:O(min(n,m))
Optimal time complexity: O(n+m)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Why use $m$ in the complexity of your algorithm but not in the complexity of the original algorithm?

  • How does the algorithm use $O(min(n, m))$ space?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You are completely right about the inconsistency between old and new Time complex:

  1. let's define n as len(first_sequence) and m as len(second_sequence), if the input sizes differ, m should be used in both.
    Original Algorithm: as explained earlier the outer loop runs n times, the inner loop runs m times, and the not in list takes up to O(n) time. Therefore, time complexity of the original code is O(n^2 \cdot m). If we assume n = m, it simplifies to O(n^3).
    New Algorithm: Converting the sequences to sets takes O(n) + O(m) time. The Python set intersection '&' average-case time complexity is O(\min(n, m)) as documented in this link :(https://wiki.python.org/moin/TimeComplexity). the overall optimal time complexity of O(n + m).

  2. Space Complexity Proof for O(\min(n, m)):
    the original algorithm allocates memory for the common_items list, an item is only appended to common_items if it exists in both first_sequence and second_sequence
    Mathematically, the intersection of two sets cannot contain more elements than the smaller of the two sets

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 2, 2026
@jamallaqdiem jamallaqdiem added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 3, 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. Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 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