MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/implement skip list - #8
Open
marissamolejon wants to merge 1 commit into
Open
MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/implement skip list#8marissamolejon wants to merge 1 commit into
marissamolejon wants to merge 1 commit 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
Implements
SkipListinskip_list.py, supportinginsert,__contains__(theinoperator), andto_list, withinsertandcontainsfaster than O(n) as required.Note: this is marked Optional / Priority Stretch on the issue - opted
to complete it as part of working through the full module.
Design
A skip list keeps multiple "levels" of linked lists stacked on the same
sorted data, where each level up contains roughly half as many elements
as the level below - similar to a book's index letting you skip
straight to a section instead of reading every page.
Each node is assigned a random height at insertion time (a repeated
coin flip, capped at
MAX_LEVEL), which is what gives the structureits logarithmic search time on average, without needing to manually
rebalance anything.
insert: searches top-down, remembering the last node visited ateach level (the
updatearray) before dropping down - these areexactly the nodes whose forward pointers need to change to splice in
the new node.
__contains__: same top-down search, checking for an exact match atlevel 0.
to_list: a single pass over level 0, which contains every elementin sorted order.
Testing
Both provided tests pass. Additionally verified locally (not part of
the committed test suite) with a 20,000-element random dataset:
to_list()output matches Python'ssorted()exactlyinproduced only a ~2.25x increase in
containstime, consistent withO(log n) rather than O(n) growth
Learning points
implementation than a balanced tree, at the cost of only offering
O(log n) average time - an accepted trade-off, the same one hash
tables make for O(1) average lookup.
__contains__(not a custom method name) is required to supportPython's
inoperator, which the provided tests rely on viaassertIn/assertNotIn.updatearray is the key bookkeeping structure forinsert-it's what turns the search phase and the splice-in phase into a
single pass, rather than searching once to find the location and
then searching again to update pointers.
Checklist