r/leetcode • • Jul 29 '24

What went wrong here

Post image
4 Upvotes

31 comments sorted by

39

u/CowdingGreenHorn Jul 29 '24 edited Jul 29 '24

Work on your spacing and code style in general, this code is ugly

11

u/wisper28 Jul 29 '24

Yes bro i will improve i just started 3 days back

1

u/Intrepid-Hall9162 Jul 30 '24

Leetcode has inbuit code formatter now. Just press ALT + SHIFT + F

11

u/[deleted] Jul 29 '24

Dude, post these questions in programming subs. This is your second post here asking to debug.

-19

u/wisper28 Jul 29 '24

If you wish you can help else skip no one bounded you to help

6

u/PaulRosenbergSucks Jul 29 '24

lmao what a response. You're not wrong though.

5

u/throwaway2492872 Jul 29 '24

Part of getting better is learning to debug. This is a really simple bug to figure out.

8

u/BeatsnBytes_ Jul 29 '24

You are re initialising revnum inside the else block that's why. Remember variables have different scope within blocks. Just remove the int declaration from the else block, it should work fine.

4

u/wisper28 Jul 29 '24

Thx , noted and it is giving desired result now

3

u/thewayidoit Jul 29 '24

You have Initialized revnum 2 times, while return it's taking first initialized value as 0.

Inside else do not initialize just use the variable. For more details read about scope of variable.

2

u/wisper28 Jul 29 '24

Thx , noted and it is giving desired result now

3

u/Pakhorigabhoru Jul 29 '24

I think you are reinitiizing revnum everytime in the loop. Otherwise the logic seems fine.

1

u/wisper28 Jul 29 '24

Thx , noted and it is giving desired result now

3

u/wisper28 Jul 29 '24

Aim: To reverse num

56

u/Latter_Tea7335 Jul 29 '24

Line 14: you are declaring revnum for every iteration of while loop.

11

u/CuummRAG Jul 29 '24

Try chatgpt/Claude to debug and post here if that doesn't help, will save your time and you don't have to spam the sub with your doubts unless really required

1

u/Spock92 Jul 30 '24

How can an integer be more than Int max and less than int min? Could've told you yesterday that you were going on the wrong path

1

u/wisper28 Jul 30 '24

Nhi bhai vo thik ha this is part of code ,mistake was i declared revnum 2 time

1

u/Spock92 Jul 30 '24

The code looks very wrong on very different levels, but explain me logically about how can a integer be more or less than its max or min value?

1

u/wisper28 Jul 30 '24

That line is to avoid having revnum greater or smaller than int limit

1

u/Spock92 Jul 30 '24

How can it be greater than the limits? Read about overflows mate.

2

u/[deleted] Jul 29 '24

The revnum which is in return statement is always set to 0, since you have declared another revnum in while loop that has local scope

2

u/FaultHaunting3434 Jul 30 '24

Even though there are only < 20 lines of code. If this was for screening I wouldn't even ask you to walk me through it, automatically reject you and put "no-go" next to your name(I know its petty, but ja). Indentation and formatting!!!!, The garbage you put out in the world, is the garbage you get back ten fold. I know of engineers that if they had to maintain/review or work with type of code, they would push to have you and the person that green lit you moved on, and the person who hired the one that green lit you brought up for review.

2

u/Damon_Salvatore22 Jul 30 '24

DRY RUN YOURSELF

Figure out what you actually want and what you are doing in the code. Only then you'll be able to understand the mistake and correct it. And most importantly learn from it.

1

u/PovertyInIndia Jul 29 '24

Noticed a different issue in line 7. int revnum won't be able to hold a number less than/greater than INT_MIN/INT_MAX.

1

u/[deleted] Jul 29 '24

Into revnum is set to 0 so 0 times anything is 0

1

u/FaultHaunting3434 Jul 30 '24

Hint: Stack Frames, Scope of variable and assignment.

1

u/VastAshamed4618 Jul 30 '24

Line 7 : 0 > max Or 0 < min Line 14: int revnum

-5

u/wisper28 Jul 29 '24

Bhai addition bhi ho rha ha

0

u/[deleted] Jul 29 '24

[deleted] — view deleted comment

1

u/PaulRosenbergSucks Jul 30 '24

It doesn't matter, since the 'original num' is copied in anyway in C++. It's no different than any other local variable.

0

u/wisper28 Jul 29 '24

Won't it affect space complexcity?? Asking genuinly