Skip to content

NW| 26-Jul-SDC | Ahmad Hmedan | Sprint 2 | Improve code with precomputing - #236

Open
AhmadHmedann wants to merge 4 commits into
CodeYourFuture:mainfrom
AhmadHmedann:improve_with_precomputing
Open

AhmadHmedann wants to merge 4 commits into
CodeYourFuture:mainfrom
AhmadHmedann:improve_with_precomputing

Conversation

@AhmadHmedann

@AhmadHmedann AhmadHmedann commented Oct 4, 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

Task code

CYF-1183

Changelist

Speed up the solutions by introducing precomputing

@github-actions

This comment has been minimized.

@AhmadHmedann AhmadHmedann added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 4, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 4, 2026
@AhmadHmedann AhmadHmedann added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 4, 2026
Comment on lines +10 to +16
seen = set()
longest = ""
for string_index, string in enumerate(strings):
for other_string in strings[string_index+1:]:
common = find_common_prefix(string, other_string)
if len(common) > len(longest):
longest = common
return longest

for string in strings:
for i in range(1, len(string) + 1):
prefix = string[:i]
if prefix in seen and len(prefix) > len(longest):
longest = prefix

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can you use complexity to explain how the new implementation is better than the original implementation?

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.

Original:

  • Time Complexity: O(N² × K)
  • Space Complexity: O(N + K)

New:

  • Time Complexity: O(N × K²)
  • Space Complexity: O(N × K)

Where:

  • N is the number of strings in the list.
  • K is the maximum length of a string.

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 time complexity analyses are correct.

Both the original and the refactored code require more space. If string length, $K$, is to be taken into consideration, an array of strings would need O(Nk) 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.

Do you mean the input space complexity?

I was calculating the auxiliary space complexity, excluding the input memory. Should I make that clear in my analysis?

I also realised that because of prefix = string[:i] and storing all the prefixes in a set, the auxiliary space complexity of the new implementation should be O(N × K²), not O(N × K).

Is my understanding correct?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you mean the input space complexity?

Yes

Should I make that clear in my analysis?

No need. Since your space complexity analyses were wrong. I was just trying to clarify.

I also realised that because of prefix = string[:i] and storing all the prefixes in a set, the auxiliary space complexity of the new implementation should be O(N × K²), not O(N × K).

Is my understanding correct?

Correct.

@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 Oct 10, 2026
@AhmadHmedann AhmadHmedann added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 11, 2026
@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 Oct 11, 2026
@AhmadHmedann AhmadHmedann added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 11, 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. labels Oct 11, 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