Leet Code interview question: reversing an integer

Hi all,

I’m green as grass and new to everything (except life - I’m quite a mature learner :wink:)

I came across the Leet Code interview questions, and am pleased to see that my newly-minted Python chops actually make me feel like I could tackle quite a few of them (certainly the ‘easy’ ones, and several of the ‘medium’ ones.

However, I was looking at this, and in my head was thinking, "Well, that looks ok - perhaps iterate over each digit, and assign to a list, perhaps get an index, and then take it away from length, or use “-1” notation, or something like that, then .join() the string at the end, and then concentrate on the other bells and whistles, like an “if” loop to test whether it’s negative, etc, and so jumped in, and found that I ran smack bang into a brick wall, because I couldn’t work out the syntax of the “class Solution(object)”

All of the class definitions I’ve used so far are more like:

class I_am_a_thing: def __init(self, start_me_up) self.start_me_up = start_me_up def __repr__(self): return "This is who I am." def i_do_something(using_this):

But the Leet Code declaration of the class is:

class Solution(object): def reverse(self, x)

and it made me realise that in all of this, I haven’t see a class declaration have a parameter. What does it do and what is it for? I feel like I’m missing something really fundamental here…

In terms of your algorithm, I’d consider space-complexity considerations. A lot of leetcode easy and mediums have traps whereas the most obvious solution is not the best for efficiency (and hence why they are popular interview questions).

When you mention creating a list (from which I assume you’d be casting the number to a string first), you end up needing extra memory usage in linear proportion to the digit-length of the integer. Now, you might argue, oh but that’s bound by a constant since integers are just going to be at most 64-bits. But there are fields where this you might often work with larger than 64 bits (science, finance, cryptography).

You might then argue, but surely there’s an upper bound (say some fixed m) on the number of bits an int would realistically use then… and the answer is probably, sure. But then you’d have to consider what if this function needs to run in parallel for multiple integers. Then the cost would become a bit much, especially if there were a better alternative.

All of this to say that you can solve this problem without using space linear to the digit-length of your integer, we can do it with some fixed small constant space.

As for the parameter…

It’s inheritance:

class DerivedClassName(BaseClassName):

and in concrete terms why object here?

Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.

It’s some bit of legacy python that’s implicit in python 3. It used to be you had.to explicitly inherit from object. It’s just saying it’s a class, that’s all.

Class Solution:

is the implicit version of

Class Solution(object):
1 Like