Skip to content

Missing Test Case - 7. Reverse Integer #32205

@lkhorasandzhian

Description

@lkhorasandzhian

LeetCode Username

yungflaeva

Problem Number, Title, and Link

  1. Reverse Integer https://leetcode.com/problems/reverse-integer/

Bug Category

Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)

Bug Description

I've written incorrect condition pop > 8 instead of as in editorial pop < -8. My code works, but it shouldn't.

Language Used for Code

C#

Code used for Submit/Run operation

public class Solution
{
    public int Reverse(int x)
    {
        int rev = 0;

        int digit;
        while (x != 0)
        {
            digit = x % 10;
            if (rev > int.MaxValue / 10 || rev == int.MaxValue / 10 && digit > 7 ||
                rev < int.MinValue / 10 || rev == int.MinValue / 10 && digit > 8)
            {
                return 0;
            }
            rev = rev * 10 + digit;
            x /= 10;
        }
        
        return rev;
    }
}

Expected behavior

First of all, this code mistake can be handled via test case only if the method would take long instead of int.

Input: -9463847412
Output [what was expected]: 0
Output [what happened]: 2147483647

Screenshots

No response

Additional context

I would change signature of the method from Reverse(int x) to Reverse(long x), so you can add test case long x = -9463847412.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions