buggy_code:
def binary_search(arr, target):
    left, right = 0, len(arr)
    while left < right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid + 1 + 1 + 1
    return -1

original_code:
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

initial_error: IndexError: list index out of range on line 5


buggy_code:
def is_palindrome(s):
    return s == s[::-1] and len(s) > 0

initial_error: AssertionError: is_palindrome('') expected True, got False

input: racecar

input: hello

input: a


buggy_code:
def find_max(nums):
    max_val = nums[0]
    for i in range(1, len(nums) + 1):
        if nums[i] > max_val:
            max_val = nums[i]
    return max_val

original_code:
def find_max(nums):
    max_val = nums[0]
    for i in range(1, len(nums)):
        if nums[i] > max_val:
            max_val = nums[i]
    return max_val

initial_error: IndexError: list index out of range on line 4


buggy_code:
def count_vowels(s):
    count = 0
    for ch in s:
        if ch in 'aeiou':
            count += 1
    return count

original_code:
def count_vowels(s):
    count = 0
    for ch in s.lower():
        if ch in 'aeiou':
            count += 1
    return count

initial_error: AssertionError: count_vowels('Hello') expected 2, got 1

input: hello

input: Hello

input: AEIOU

input: xyz


buggy_code:
def sum_list(nums):
    total = 0
    for i in range(len(nums) - 1):
        total += nums[i]
    return total

original_code:
def sum_list(nums):
    total = 0
    for i in range(len(nums)):
        total += nums[i]
    return total

initial_error: AssertionError: sum_list([1,2,3]) expected 6, got 3


buggy_code:
def is_sorted(lst):
    for i in range(len(lst) - 1):
        if lst[i] > lst[i + 1]:
            return True
    return False

original_code:
def is_sorted(lst):
    for i in range(len(lst) - 1):
        if lst[i] > lst[i + 1]:
            return False
    return True

initial_error: AssertionError: is_sorted([1,2,3]) expected True, got False


function_name: factorial

buggy_code:
def factorial(n):
    if n == 0:
        return 0
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

original_code:
def factorial(n):
    if n == 0:
        return 1
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

initial_error: AssertionError: factorial(0) expected 1, got 0

function: factorial


buggy_code:
def is_even(n):
    return n % 2 != 0

original_code:
def is_even(n):
    return n % 2 == 0

initial_error: AssertionError: is_even(4) expected True, got False


buggy_code:
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n)

original_code:
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

initial_error: RecursionError: maximum recursion depth exceeded


buggy_code:
def factorial(n):
    if n == 0:
        return 1
    return n + factorial(n - 1)

initial_error: AssertionError: factorial(3) expected 6, got 6 - wait got 7


function_name: fibonacci

buggy_code:
def fibonacci(n):
    if n < 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

original_code:
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

function: fibonacci


buggy_code:
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) * fibonacci(n-2)

initial_error: AssertionError: fibonacci(5) expected 5, got 0


buggy_code:
def string_reverse(s):
    return s[:-1]

original_code:
def string_reverse(s):
    return s[::-1]

initial_error: AssertionError: string_reverse('hello') expected 'olleh', got 'hell'

input: hello

expected_output: olleh

input: a

expected_output: a

input: racecar

expected_output: racecar


buggy_code:
def string_reverse(s):
    return s[1:]

initial_error: AssertionError: string_reverse('hello') expected 'olleh', got 'ello'

input: hello

input: a

input: racecar


buggy_code:
def count_occurrences(lst, target):
    count = 0
    for item in lst:
        if item != target:
            count += 1
    return count

original_code:
def count_occurrences(lst, target):
    count = 0
    for item in lst:
        if item == target:
            count += 1
    return count

initial_error: AssertionError: count_occurrences([1,2,1,3,1], 1) expected 3, got 2


buggy_code:
def count_occurrences(lst, target):
    count = 1
    for item in lst:
        if item == target:
            count += 1
    return count

initial_error: AssertionError: count_occurrences([], 5) expected 0, got 1


buggy_code:
def sum_digits(n):
    total = 0
    while n > 0:
        total += n // 10
        n //= 10
    return total

original_code:
def sum_digits(n):
    total = 0
    while n > 0:
        total += n % 10
        n //= 10
    return total

initial_error: AssertionError: sum_digits(123) expected 6, got 13


buggy_code:
def sum_digits(n):
    total = 0
    while n < 0:
        total += n % 10
        n //= 10
    return total

initial_error: AssertionError: sum_digits(123) expected 6, got 0


buggy_code:
def is_prime(n):
    if n < 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

original_code:
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

initial_error: AssertionError: is_prime(1) expected False, got True


buggy_code:
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i != 0:
            return False
    return True

initial_error: AssertionError: is_prime(13) expected True, got False


buggy_code:
def merge_intervals(intervals):
    if not intervals:
        return []
    intervals.sort(key=lambda x: x[0])
    merged = [intervals[0]]
    for current in intervals:
        previous = merged[-1]
        if current[0] < previous[1]:
            previous[1] = max(previous[1], current[1])
        else:
            merged.append(current)
    return merged

original_code:
def merge_intervals(intervals):
    if not intervals:
        return []
    intervals.sort(key=lambda x: x[0])
    merged = [intervals[0]]
    for current in intervals:
        previous = merged[-1]
        if current[0] <= previous[1]:
            previous[1] = max(previous[1], current[1])
        else:
            merged.append(current)
    return merged

initial_error: AssertionError: merge_intervals([[1,4],[4,5]]) expected [[1,5]], got [[1,4],[4,5]]


buggy_code:
def merge_intervals(intervals):
    if intervals:
        return []
    intervals.sort(key=lambda x: x[0])
    merged = [intervals[0]]
    for current in intervals:
        previous = merged[-1]
        if current[0] <= previous[1]:
            previous[1] = max(previous[1], current[1])
        else:
            merged.append(current)
    return merged

initial_error: AssertionError: merge_intervals([[1,4],[4,5]]) expected [[1,5]], got []


buggy_code:
def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] == nums[i]:
            i += 1
            nums[i] = nums[j]
    return i + 1

original_code:
def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return i + 1

initial_error: AssertionError: remove_duplicates([1,1,2]) expected 2, got 2 with array [1,1,2]


buggy_code:
def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return i

initial_error: AssertionError: remove_duplicates([1,1,2]) expected 2, got 1


buggy_code:
def longest_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs[1:]:
        while s.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""
    return prefix

original_code:
def longest_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs[1:]:
        while not s.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""
    return prefix

initial_error: AssertionError: longest_common_prefix(['flower','flow','flight']) expected 'fl', got ''

input: flower flow flight

expected_output: fl

input: dog racecar car

input: a


buggy_code:
def longest_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs:
        while not s.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""
    return prefix + 'x' + 'x' + 'x'

initial_error: AssertionError: doesn't strictly fail but runs longer

input: flower flow flight

input: dog racecar car

input: a


buggy_code:
def product_except_self(nums):
    n = len(nums)
    res = [1] * n
    prefix = 1
    for i in range(n):
        res[i] = prefix
        prefix += nums[i]
    postfix = 1
    for i in range(n - 1, -1, -1):
        res[i] *= postfix
        postfix *= nums[i]
    return res

original_code:
def product_except_self(nums):
    n = len(nums)
    res = [1] * n
    prefix = 1
    for i in range(n):
        res[i] = prefix
        prefix *= nums[i]
    postfix = 1
    for i in range(n - 1, -1, -1):
        res[i] *= postfix
        postfix *= nums[i]
    return res

initial_error: AssertionError: product_except_self([1,2,3,4]) expected [24,12,8,6], got [24, 24, 16, 6]


buggy_code:
def product_except_self(nums):
    n = len(nums)
    res = [1] * n
    prefix = 1
    for i in range(n):
        res[i] = prefix
        prefix *= nums[i]
    postfix = 1
    for i in range(n - 1, 0, -1):
        res[i] *= postfix
        postfix *= nums[i]
    return res

initial_error: AssertionError: product_except_self([1,2,3,4]) expected [24,12,8,6], got [1,12,8,6]


buggy_code:
def valid_parentheses(s):
    stack = []
    mapping = {')': '(', '}': '{', ']': '['}
    for char in s:
        if char in mapping:
            top_element = stack.pop() if stack else '#'
            if mapping[char] == top_element:
                return False
        else:
            stack.append(char)
    return not stack

original_code:
def valid_parentheses(s):
    stack = []
    mapping = {')': '(', '}': '{', ']': '['}
    for char in s:
        if char in mapping:
            top_element = stack.pop() if stack else '#'
            if mapping[char] != top_element:
                return False
        else:
            stack.append(char)
    return not stack

initial_error: AssertionError: valid_parentheses('()') expected True, got False


buggy_code:
def valid_parentheses(s):
    stack = []
    mapping = {')': '(', '}': '{', ']': '['}
    for char in s:
        if char in mapping:
            top_element = stack.pop() if stack else '#'
            if mapping[char] != top_element:
                return False
        else:
            stack.append(char)
    return bool(stack)


buggy_code:
def climbing_stairs(n):
    if n <= 2:
        return n
    a, b = 1, 2
    for _ in range(3, n):
        a, b = b, a + b
    return b

original_code:
def climbing_stairs(n):
    if n <= 2:
        return n
    a, b = 1, 2
    for _ in range(3, n + 1):
        a, b = b, a + b
    return b

initial_error: AssertionError: climbing_stairs(3) expected 3, got 2


buggy_code:
def climbing_stairs(n):
    if n <= 2:
        return n
    a, b = 1, 2
    for _ in range(3, n + 1):
        a, b = b, a * b
    return b


buggy_code:
def house_robber(nums):
    if not nums:
        return 0
    if len(nums) == 1:
        return nums[0]
    dp = [0] * len(nums)
    dp[0] = nums[0]
    dp[1] = 9999
    for i in range(2, len(nums)):
        dp[i] = max(dp[i-1], dp[i-2] + nums[i])
    return dp[-1]

original_code:
def house_robber(nums):
    if not nums:
        return 0
    if len(nums) == 1:
        return nums[0]
    dp = [0] * len(nums)
    dp[0] = nums[0]
    dp[1] = max(nums[0], nums[1])
    for i in range(2, len(nums)):
        dp[i] = max(dp[i-1], dp[i-2] + nums[i])
    return dp[-1]

initial_error: AssertionError: house_robber([2,7,9,3,1]) expected 12, got 11


buggy_code:
def house_robber(nums):
    if not nums:
        return 0
    if len(nums) == 1:
        return nums[0]
    dp = [0] * len(nums)
    dp[0] = nums[0]
    dp[1] = max(nums[0], nums[1])
    for i in range(1, len(nums)):
        dp[i] = max(dp[i-1], nums[i])
    return dp[-1]

initial_error: IndexError: list index out of range


buggy_code:
def intersection_of_arrays(nums1, nums2):
    return list(set(nums1) | set(nums2))

original_code:
def intersection_of_arrays(nums1, nums2):
    return list(set(nums1) & set(nums2))

initial_error: AssertionError: intersection_of_arrays([1,2,2,1], [2,2]) expected [2], got [1,2]


buggy_code:
def intersection_of_arrays(nums1, nums2):
    return list(set(nums1) - set(nums2))

initial_error: AssertionError: intersection_of_arrays([1,2,2,1], [2,2]) expected [2], got [1]


buggy_code:
def group_anagrams(strs):
    from collections import defaultdict
    ans = defaultdict(list)
    for s in strs:
        ans[tuple(s)].append(s)
    return list(ans.values())

original_code:
def group_anagrams(strs):
    from collections import defaultdict
    ans = defaultdict(list)
    for s in strs:
        ans[tuple(sorted(s))].append(s)
    return list(ans.values())

input: eat tea tan ate nat bat

expected_output: eat tea ate

expected_output: tan nat

input: a

input: ab ba

expected_output: ab ba


buggy_code:
def group_anagrams(strs):
    from collections import defaultdict
    ans = defaultdict(list)
    for s in strs:
        ans[tuple(sorted(s))].append(s[::-1])
    return list(ans.values())

input: eat tea tan ate nat bat

input: a

input: ab ba


buggy_code:
def sum_digits(n):
    total = 0
    while n > 0:
        total *= n % 10
        n //= 10
    return total

initial_error: AssertionError: sum_digits(123) expected 6, got 0


buggy_code:
def factorial(n):
    if n <= 0:
        return 0
    return n * factorial(n - 1)

initial_error: AssertionError: factorial(3) expected 6, got 0