Skip to content

West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186

Closed
Iswanna wants to merge 10 commits into
CodeYourFuture:mainfrom
Iswanna:analyse-and-refactor-functions
Closed

West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186
Iswanna wants to merge 10 commits into
CodeYourFuture:mainfrom
Iswanna:analyse-and-refactor-functions

Conversation

@Iswanna

@Iswanna Iswanna commented Jun 29, 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

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

  • Logic Optimization: Replaced nested for loops and linear array searches (.includes()/in list) with Hash Set lookups.
  • Performance Gain: Optimized three out of four tasks from $O(n^2)$ to Linear Time ($O(n)$) by leveraging $O(1)$ constant-time membership checks.
  • Loop Refactoring: Combined separate loops into a single pass to reduce unnecessary iterations.
  • Documentation: Added comprehensive CHANGES-MADE.md files in both language directories to document the specific complexity trade-offs for each task.

Key Learnings

  • Space-Time Trade-off: I practiced using additional memory (Space) to store Sets in exchange for significantly faster execution (Time).
  • Pythonic Best Practices: Learned to avoid "variable shadowing" (renaming sum to total_sum) and implemented the more efficient in keyword for Python sets.
  • Maintainability: Used descriptive variable names like remaining_number_needed to make the underlying mathematical logic (finding complements) clear to other developers.

Verification

  • All JavaScript tests passed using node and npm test.
  • All Python tests passed using pytest.

Iswanna added 10 commits June 27, 2026 20:55
- 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
@Iswanna Iswanna 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 29, 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.

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)))];

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

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 just use either total or sum.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jun 30, 2026
@illicitonion

Copy link
Copy Markdown
Member

Closing PR because the SDC run has finished. Feel free to re-open if you're still working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants