LeetCode-Feedback / LeetCode-Feedback

665 stars 321 forks source link

[BUG] - Incorrect constraints or test case #22890

Closed Uni-Bo closed 3 months ago

Uni-Bo commented 3 months ago

LeetCode Username

Debanjan_Konar_0408

Problem Number, Title, and Link

  1. Find Center of Star Graph https://leetcode.com/problems/find-center-of-star-graph/

Bug Category

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

Bug Description

Given Constraints: 3 <= n <= 105 edges.length == n - 1 edges[i].length == 2 1 <= ui, vi <= n ui != vi The given edges represent a valid star graph. are not valid for test case 59/60

Language Used for Code

C++

Code used for Submit/Run operation

++ []
class Solution {
public:
    int findCenter(vector<vector<int>>& edge) {
        unordered_map<int,int>count;
        {
            for(int i=0;i<3;i++)
            {
                count[edge[i][0]]++;
                count[edge[i][1]]++;
            }
        }
        for(auto& it:count)
        {
            if(it.second>=3)
                return it.first;
        }
        return -1;
    }
};

Expected behavior

class Solution {
public:
    int findCenter(vector<vector<int>>& edge) {
        unordered_map<int,int>count;
        {
            for(int i=0;i<3;i++)
            {
                count[edge[i][0]]++;
                count[edge[i][1]]++;
            }
        }
        for(auto& it:count)
        {
            if(it.second>=3)
                return it.first;
        }
        return -1;
    }
};

should provide an output for every case that has n>=3. but test case 59/60 has values: edges=[[1,3],[3,2]]. which gives a heap-buffer-overflow error

Screenshots

Screenshot 2024-06-27 084555

Additional context

No response

exalate-issue-sync[bot] commented 3 months ago

Winston Tang commented: Hello Debanjan_Konar_0408, Thank you for reaching out. We understand that you're finding it challenging working with this problem. However, after thoroughly analyzing your concern, it seems the difficulty results from a misunderstanding in your code. The issue you're experiencing arises by assuming the 'edges' vector will always have at least three elements, which is not guaranteed by the problem constraints. Therefore, your code may induce an out-of-bounds error, as seen in test case 59/60. Please reassess your logic and assumptions for the code and try again. Remember, the constraints mentioned in the problem statement guide you on how to generalize your solution for any valid input. Good luck, and keep practicing! Should you have any other questions or need further clarification, don't hesitate to ask. Best Regards, LeetCode Support Team