MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | Feat/improve with precomputing - #7
Open
marissamolejon wants to merge 2 commits into
Open
MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | Feat/improve with precomputing#7marissamolejon wants to merge 2 commits into
marissamolejon wants to merge 2 commits into
Conversation
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.
Summary
Speeds up both functions in
improve_with_precomputingby precomputingdata once up front, avoiding redundant work per element.
Changes made
find_longest_common_prefixcount_lettersDesign notes
find_longest_common_prefixrelies on a property of sorted order: twostrings sharing a long common prefix are always adjacent (or separated
only by other strings sharing that same prefix) once sorted. This
guarantees the true maximum is found by checking only n-1 adjacent
pairs, instead of all n*(n-1)/2 pairs.
count_lettersprecomputes which lowercase letters appear anywhere inthe string in one pass, turning each subsequent "does this letter's
lowercase form appear in the string" check from an O(n) scan into an
O(1) set lookup.
Testing
All existing tests pass unchanged, including the large-input tests
(
test_really_long_list, 1,000,000 strings;test_long_string,10,000,000 characters).
Note:
test_long_string's own setup code builds its input via repeatedstring concatenation (
s += next) in a loop, which is a known slowpattern for building large strings and is unrelated to
count_lettersitself - this made the test slow to run locally, but
count_letterswas independently verified to run in under a second on an equivalent
10-million-character input built a different way.
Learning points
work happens once, up front, rather than being repeated per element -
sorting once instead of comparing every pair is a strong example of
this.
sufficient) is as important as the complexity win itself - it's not
enough for an optimization to be fast, it has to still be provably
correct.
the function under test - worth diagnosing which part is actually slow
before assuming the implementation is at fault.
Checklist