Skip to content

Bug Report for dynamicArray - Need more test cases #5813

@mufaddal404

Description

@mufaddal404

Bug Report for https://neetcode.io/problems/dynamicArray

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Below is the popback() method written in the solution of the problem. The method makes sure that self.length doesn't go out of bounds (becomes -ve).
Image

In my attempt, I didn't check for that and allowed self.size (equivalent to self.length) to have negative values(that was error on my part) and my submission was successful.
Image

I believe you guys need to add test cases to cover this bad behavior.

Below is my complete code:

class DynamicArray:
    
    def __init__(self, capacity: int):
        self.array = [None]*capacity
        self.capacity = capacity
        self.size = 0

    def get(self, i: int) -> int:
        return self.array[i]

    def set(self, i: int, n: int) -> None:
        self.array[i] = n

    def pushback(self, n: int) -> None:
        if self.size == self.capacity:
            self.resize()
        self.array[self.size] = n
        self.size += 1

    def popback(self) -> int:
        self.size -= 1
        el = self.array[self.size]
        return el

    def resize(self) -> None:
        self.capacity *= 2
        new_arr = [None] * self.capacity
        for i in range(self.size):
            new_arr[i] = self.array[i]
        self.array = new_arr

    def getSize(self) -> int:
        return self.size
    
    def getCapacity(self) -> int:
        return self.capacity

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions