Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sprint-2/implement_skip_list/skip_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class SkipList:
def __init__(self):
self.data = []

def insert(self, value):
"""Insert value in sorted order"""
# Find insertion point
left, right = 0, len(self.data)
while left < right:
mid = (left + right) // 2
if self.data[mid] < value:
left = mid + 1
else:
right = mid

# Insert at found position
self.data.insert(left, value)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Although you've found the insertion point in O(log(n)) this insert call is O(n), so your overall insert is O(n), which doesn't meet requirements

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.

Fixed! Replaced the list-based implementation with a proper skip list using linked nodes and multiple levels. This achieves O(log n) insertion by updating pointers instead of using list.insert() which is O(n).


def contains(self, value) -> bool:
"""Check if value exists"""
left, right = 0, len(self.data)
while left < right:
mid = (left + right) // 2
if self.data[mid] == value:
return True
elif self.data[mid] < value:
left = mid + 1
else:
right = mid
return False

def to_list(self) -> list:
"""Convert to sorted list"""
return self.data.copy()

def __contains__(self, value) -> bool:
return self.contains(value)