Question: 
Write a function to check if the letters of a given string can be rearranged so that two characters that are adjacent to each other are different.


Model's Code:
def rearrange_string(s):
    from collections import Counter
    from heapq import heappop, heappush

    count = Counter(s)
    max_heap = []
    
    for char, freq in count.items():
        heappush(max_heap, (-freq, char))
    
    result = []
    prev_char = None
    prev_freq = 0
    
    while max_heap:
        freq, char = heappop(max_heap)
        result.append(char)
        
        if prev_freq < 0:
            heappush(max_heap, (prev_freq, prev_char))
        
        prev_char = char
        prev_freq = freq + 1
    
    return ''.join(result) if len(result) == len(s) else ''



Sample Correct Code:
import heapq
from collections import Counter
def rearange_string(S):
    ctr = Counter(S)
    heap = [(-value, key) for key, value in ctr.items()]
    heapq.heapify(heap)
    if (-heap[0][0]) * 2 > len(S) + 1: 
        return ""
    ans = []
    while len(heap) >= 2:
        nct1, char1 = heapq.heappop(heap)
        nct2, char2 = heapq.heappop(heap)
        ans.extend([char1, char2])
        if nct1 + 1: heapq.heappush(heap, (nct1 + 1, char1))
        if nct2 + 1: heapq.heappush(heap, (nct2 + 1, char2))
    return "".join(ans) + (heap[0][1] if heap else "")


Asserts:
assert rearange_string("aab")==('aba')
assert rearange_string("aabb")==('abab')
assert rearange_string("abccdd")==('cdabcd')


Tests:
Test 1 of 3: FAIL: NameError("name 'rearange_string' is not defined")

Test 39 of 974: 30/39 (76.92307692307693%): failed