Bug Report for dynamicArray
Bug Report for https://neetcode.io/problems/dynamicArray
Hello, I’m currently having issues with this problem.
My solution seems correct: it passes all the local tests I run, but when I submit, it shows an error. The error doesn’t appear to be accurate, since I tried debugging by running the exact same sequence of commands from the submission test, and everything worked as expected. I’m not sure why it’s failing only in submission.
Evidence from Tests
As you can see below, getCapacity and the other functions worked just fine in my local tests:
But on submission, the platform reports an error:
Debugging
I tried to replicate the exact same submission test locally to investigate. And for my surprise… in my machine worked™ 😅
Code
Here’s the full implementation I’m submitting:
class DynamicArray:
arr = []
def __init__(self, capacity: int):
if capacity < 1:
self.arr = []
else:
for i in range(capacity):
self.arr.append(None)
def get(self, i: int) -> int:
return self.arr[i]
def set(self, i: int, n: int) -> None:
self.arr[i] = n
def pushback(self, n: int) -> None:
if self.getSize() >= self.getCapacity():
self.resize()
for i, item in enumerate(self.arr):
if item is None:
self.arr[i] = n
return
def popback(self) -> int:
last_index = self.getSize() - 1
deleted_value = self.arr[last_index]
self.arr[last_index] = None
return deleted_value
def resize(self) -> None:
old_capacity = len(self.arr)
new_capacity = old_capacity * 2 if old_capacity > 0 else 1
self.arr.extend([None] * (new_capacity - old_capacity))
def getSize(self) -> int:
filtered_arr = []
for item in self.arr:
if item is not None:
filtered_arr.append(item)
return len(filtered_arr)
def getCapacity(self) -> int:
return len(self.arr)