Leetcode Silliness

So I was looking at some reviews for a company I would like to work for- one of the reviews mentioned they took their interview questions from leetcode, so I took a peek.

I did a couple of their exercises, and one in particular was interesting to me.

https://leetcode.com/problems/valid-number/

Essentially the purpose was to validate a string that has an integer value. Easy street! Numbers are simple. Well their requirements were quite odd, and it led to several frustrating iterations, which felt unnecessary. For instance " -5" is a number but " - 5" is not a number, but there were no written guidelines indicating leading or trailing spaces should be ignored while inner spaces should be invalid. No big deal, got it figured out, moved on to the next logical question:

https://leetcode.com/problems/string-to-integer-atoi/

A custom atoi.

Now this one is much more fun! Normally I would use stringstream, but the requirements from the last batch were strange, so the requirements for this one ought to be strange too right?

So naturally I spent a bit and wrote a function (which is quite handy anyways) that converts a string that has any integer style value to that value (so 0x55, 12e-9, -.55, the works!)

I was a bit miffed when the requirements were nothing more than +-xxxxxxxxxxxxx but at least I got a useful function out of the deal!

It led me to thinking about a couple things. Is there a database of self contained functions somewhere online? Maybe a website that you can type a few key search words and get a list of functions tagged with them?

For instance, I wrote a little json/xml/csv conversion class/function set- I don't really want to release a library, and I would feel a bit silly releasing it on my website or github, but it would be great to submit it to a big free function database or something!

Obviously we have sites like this and stackoverflow and the other QA sites. All of which are logical places. It would be really nice to have just code somewhere though!

Anyone know of a site like that? I may just go ahead and make it myself if it doesn't exist, but I like minimizing duplicated efforts (I suppose that's obvious)
There used to be the "Snippets" collection maintained by Bob Stout (who started collecting snippets somewhere in the 90's.) This post reminded me of that so I went looking for 'em. Bob is dead now, but it looks like someone tried to preserve the collection.

https://github.com/vonj/snippets

Some interesting old stuff in there.

Edit: http://web.archive.org/web/20080217222209/http://c.snippets.org/browser.php
has a summary of what's in the files.
Last edited on
Very cool stuff! I think I may just go ahead and make a site/api for this- I'll let you know when I've finished!
I think I'd run for the hills if I saw either of these two questions in an interview. Why? Because passing the arg as a string instead of a const string & or const char * is very inefficient.

[Edit: the cases where you'd want to use const char* instead of const string & are few and far between. See posts below. ]
Last edited on
I absolutely agree- if that's the basis of a test question the existing code base is probably in nightmare territory. To be fair, the website does accept answers using a const string& instead.
> Why? Passing the arg as a string instead of a const string & or const char * is very inefficient.
>> if that's the basis of a test question the existing code base is probably in nightmare territory.

The C++ community doesn’t need another generation of programmers who by default use the lowest level of language and library facilities available out of misplaced fear of inefficiencies.
http://www.cplusplus.com/forum/lounge/183836/#msg899457

Hint: copy-elision, move semantics, validation follows input.
The C++ community doesn’t need another generation of programmers who by default use the lowest level of language and library facilities available out of misplaced fear of inefficiencies.

The same quote says that programs should
descend to lower levels of abstraction only where absolutely necessary.
To me, the subject problems (extracting and validating an integer in text) is such a low level operation that might warrant a solution that doesn't force the program to include std::string. One might find that useful, for example, in a micro-controller where space is very tight.

But in general, of course you're right. I've edited my post.
Searching copy-elision turns up a compiler optimization

TLDR; The compiler can optimize the mechanism that would normally copy an object, and not copy it- so long as the results are identical. This makes the whole process much much faster and looser than strictly adhering to copy by value vs reference syntax.

The C++ community doesn’t need another generation of programmers who by default use the lowest level of language and library facilities available out of misplaced fear of inefficiencies.


I think the community will live through some of us having misplaced fears because we lack understanding- unfortunately you can't expect us to know everything about compiler optimizations, so try to take it easy on us! Thank you for the information of course, it's handy and neat.

Habits die hard I'm afraid, you'll be stuck seeing a whole lot of copying by reference if you have the misfortune of trodding upon my future code playground.
I would also pass by reference to const - const std::string& - in this case.
Topic archived. No new replies allowed.