West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186
Closed
Iswanna wants to merge 10 commits into
Closed
West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186Iswanna wants to merge 10 commits into
Iswanna wants to merge 10 commits into
Conversation
- Refactored logic to calculate both sum and product in one pass - Reduced iterations from 2n to n while maintaining O(n) time complexity - Maintained O(1) space complexity
- Replaced Array.includes (O(m)) with Set.has (O(1)) inside the filter loop - Improved time complexity from O(n * m) to O(n + m) - Utilized a Set for precomputing lookups to avoid nested linear searches
- Replaced nested loops (O(n^2)) with a single-pass approach - Implemented a Set to store visited numbers for O(1) complement (remainingNumberNeeded) lookups - Improved performance for large datasets by trading space (O(n)) for time (O(n))
- Replaced nested loops (O(n^2)) with a Set-based lookup approach - Achieved O(n) time complexity by utilizing O(1) Set.has() checks - Maintained the original order of elements while improving scalability - Increased space complexity to O(n) to store the Set of seen elements
…vaScript - Added CHANGES-MADE.md to document performance improvements - Summarized the transition from O(n^2) to O(n) across JavaScript tasks - Explained the Space-Time trade-offs involved in using Sets for optimization
- Combined sum and product calculations into a single for-loop - Renamed 'sum' variable to 'total_sum' to avoid shadowing Python's built-in sum() - Maintained O(n) time complexity while reducing operations from 2n to n - Maintained O(1) space complexity
- Replaced nested loops (O(n * m)) with a Set for constant time lookups - Converted second_sequence to a set to enable O(1) membership checks - Utilized a result set to ensure uniqueness without linear searches in a list - Improved time complexity from quadratic to linear (O(n + m))
- Replaced nested O(n^2) loops with a single-pass hash set approach - Implemented 'remaining_number_needed' logic for O(1) membership lookups - Improved time complexity from quadratic to linear - Accepted O(n) space complexity as a trade-off for significantly faster execution
- Replaced nested loops (O(n^2)) with a single-pass approach using a Set - Leveraged O(1) membership checks with 'element_seen' to identify duplicates - Utilized a result list to preserve the original order of first occurrences - Documented O(n) time and space complexity in the docstring
- Documented refactoring results for the 4 Python exercises - Explained the efficiency of Python sets vs lists for membership checks - Highlighted the transition from quadratic to linear time complexity
cjyuan
reviewed
Jun 30, 2026
cjyuan
left a comment
There was a problem hiding this comment.
Well formatted and clear complexity analysis.
Code looks good. Excellent job!
| export const findCommonItems = (firstArray, secondArray) => { | ||
| const secondArraySet = new Set(secondArray); | ||
|
|
||
| return [...new Set(firstArray.filter((item) => secondArraySet.has(item)))]; |
There was a problem hiding this comment.
Could also take advantage of Set's build-in methods. Set implementation in most programming languages typically supports standard set operations like union, intersect, diff, etc.
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| total_sum = 0 |
Member
|
Closing PR because the SDC run has finished. Feel free to re-open if you're still working on it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Learners, PR Template
Self checklist
Changelist
This PR completes the Sprint 1 module focused on Time and Space Complexity. I have refactored 4 programming tasks in both JavaScript and Python to improve their algorithmic efficiency.
Technical Changes
The primary focus of this refactor was identifying and eliminating Quadratic Time Complexity
O(n²).forloops and linear array searches (.includes()/in list) with Hash Set lookups.CHANGES-MADE.mdfiles in both language directories to document the specific complexity trade-offs for each task.Key Learnings
sumtototal_sum) and implemented the more efficientinkeyword for Python sets.remaining_number_neededto make the underlying mathematical logic (finding complements) clear to other developers.Verification
nodeandnpm test.pytest.