LeetCode-Feedback / LeetCode-Feedback

672 stars 331 forks source link

[BUG] - test case passes and fails #24689

Closed traumfeuchte closed 3 weeks ago

traumfeuchte commented 4 weeks ago

LeetCode Username

mTL03VxOp1

Problem Number, Title, and Link

228, summary ranges https://leetcode.com/problems/summary-ranges/description/?source=submission-noac

Bug Category

Incorrect test case (Output of test case is incorrect as per the problem statement)

Bug Description

test case passes and fails, just depending on the order of test cases:

when test is first it passes when test is not first test case: it may fail

output displayed depends on the previous test case

Language Used for Code

C

Code used for Submit/Run operation

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** summaryRanges(int* nums, int numsSize, int* returnSize) {

char ** result;

    if (numsSize > 0) {

        result = (char**)malloc(sizeof(char*) * 20);

        // get 2 digits
        // compare to incr
        //  if equal continue range else open new range

        int ranges = 0;
        int incr = nums[0] - 1;
        int range_count = 0;
        int i = 0;

        while (i < numsSize) {
            incr++;
            if (incr == nums[i]) {
                range_count++;
            } else {
                result[ranges] = (char*)malloc(sizeof(char) * 28);
                if (range_count > 1) {
                    snprintf(result[ranges], 28, "%d->%d",
                             nums[i - range_count], nums[i - 1]);
                } else {
                    snprintf(result[ranges], 28, "%d", nums[i - 1]);
                }
                ranges++;
                range_count = 1;
                incr = nums[i];
            }
            i++;
        }
        result[ranges] = (char*)malloc(sizeof(char) * 28);
        if (range_count > 1) {
            snprintf(result[ranges], 28, "%d->%d", nums[i - range_count],
                     nums[i - 1]);
        } else {
            snprintf(result[ranges], 28, "%d", nums[i - 1]);
        }
        ranges++;
        *returnSize = ranges;
 return result;
    }

 return result;
}

Expected behavior

expected: test case of input empty array passes when its the first/only test case but fails otherwise

Screenshots

1 2

Additional context

No response

exalate-issue-sync[bot] commented 4 weeks ago

LeetCode Support commented: Hi mTL03VxOp1,

Thank you for reaching out and providing detailed information on the issue you're experiencing. Based on the information you've submitted, it appears the issue is related to how the test cases are executed and the memory is managed in your code. C language requires careful handling of memory allocation and initialization, especially between consecutive test runs.

It is often helpful to ensure that all pointers and arrays are properly initialized or reallocated between separate test cases. Additionally, you might want to explicitly handle cases where the input is an empty array to prevent unexpected behavior.

If you are encountering persistent issues, please try verifying your code’s logic and memory allocation routines. Alternatively, consulting forums or resources on C language memory management could provide more insight into resolving this issue.

Feel free to reach out if you have further questions or encounter any other issues. We're here to help!

LeetCode Support Team