Skip to content

London | 25-SDC-NOV | Jesus del Moral | Sprint 1 | Analyse and Refactor Functions#94

Open
delmorallopez wants to merge 4 commits intoCodeYourFuture:mainfrom
delmorallopez:complexity
Open

London | 25-SDC-NOV | Jesus del Moral | Sprint 1 | Analyse and Refactor Functions#94
delmorallopez wants to merge 4 commits intoCodeYourFuture:mainfrom
delmorallopez:complexity

Conversation

@delmorallopez
Copy link
Copy Markdown

@delmorallopez delmorallopez commented Feb 9, 2026

No description provided.

@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 9, 2026
Copy link
Copy Markdown

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

Very detailed analysis.

* Optimal Time Complexity: O(n) - We must examine each element at least once to calculate the sum and product, so O(n) is the best we can achieve for this problem.
*
* we can reduce the constant factor by eliminating the redundant second loop
* Instead of two passes (2n operations), we can do both calculations in a single pass (n operations)
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 do you mean by "two passes == 2n operations" and "1 pass == n operation"?
What are these operations? Does that mean one pass is guaranteed to take half as much time to complete?

The code still needs to perform sum += num and product *= num whether we use one loop or two loops.

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.

When I said “2n vs n operations”, I meant loop iterations. However, both implementations still perform n additions and n multiplications. The single-loop version reduces loop overhead and improves cache efficiency, but asymptotically both are O(n). One pass is not guaranteed to take half the time, because Big-O ignores constant factors and the arithmetic work remains the same.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You have a good understanding and the performance is indeed improved when two loops are reduced to one.
However, the improvement is not really "doubled", so in complexity analysis we don't focus on the constant factor.

common_items.append(i)
return common_items

return list(set(first_sequence) & set(second_sequence))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Clean solution!

@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 Mar 2, 2026
@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 2, 2026
@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. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 2, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants