Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
b0lt
Apr 29, 2005

FamDav posted:

There is no known linear time algorithm for Fibonacci that doesn't assume a machine that we cannot build.

There are no non-constant time terminating algorithms that don't assume a machine that we cannot build.

Adbot
ADBOT LOVES YOU

b0lt
Apr 29, 2005

Centripetal Horse posted:

Wouldn't this do the job?

code:
int valueToTest = 212;
int bitsSet = 0;
while (valueToTest > 0) {
        bitsSet += valueToTest & 1;
        valueToTest >>= 1;
}
Maybe I am misunderstanding, because that seems way too easy to be tripping up anyone who has developed anything more complicated than Wordpress themes.

Now I really want a thread where one of you hiring managers posts all the tests you've had huge failure rates on so we goons can take stabs at the problems.

Aside from not working for negative values, yeah, this works.

If I were giving this as an interview question, it'd be as the first stage of a multistep problem, though. How would you make this faster?

b0lt
Apr 29, 2005

hobbesmaster posted:

You can use it anywhere technically, https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
I'm not sure its ever a good idea though?

It's illegal in a constructor for a type with non-trivial destructor, and in destructors always:

3.8.1 posted:

The lifetime of an object of type T begins when:
— storage with the proper alignment and size for type T is obtained, and
— if the object has non-trivial initialization, its initialization is complete

The lifetime of an object of type T ends when:
— if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

3.8.5 posted:

Before the lifetime of an object has started but after the storage which the object will occupy has been
allocated40 or, after the lifetime of an object has ended and before the storage which the object occupied is
reused or released, any pointer that refers to the storage location where the object will be or was located
may be used but only in limited ways.
[...]
The program has undefined behavior if:
— the object will be or was of a class type with a non-trivial destructor and the pointer is used as the
operand of a delete-expression

12.4.15 posted:

Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the
destructor is invoked for an object whose lifetime has ended (3.8).

b0lt
Apr 29, 2005

hobbesmaster posted:

Then thats #define class struct
And it should always work.

code:
#ifdef class
#error lol
#endif

b0lt
Apr 29, 2005

hobbesmaster posted:

Unions are also classes if we're talking about C++ trivia.

I have no idea why.

More fun trivia: typedef is a storage class.

Adbot
ADBOT LOVES YOU

b0lt
Apr 29, 2005

Hammerite posted:

I think if a valid answer to a question is "I don't know, why the hell would you do that in the first place" (as in the case of the "#define class struct" thing) then it is not a suitable interview question. I mean, if it's never going to come up with a sane team writing sane code then why do you care whether the candidate can work out on the spot what would happen? I think the examples of questions you gave in your post are solid, with the possible exception of the faulty machines one.

You should never do #define class struct, but you should be able to reason about what it would do, because you know the difference between a class and a struct.

  • Locked thread