Live data from Hacker News

Computer Science Interview Questions with C++ Solutions

grokit.ca

11–20 of 73 posts

Re: Computer Science Interview Questions with C++ Solutions

#11

Earlier quoted context omitted.

Because neither is a language commonly used in interviews.

Maybe they're not commonly used in interviews because too few people refuse to take the sort of interviews where they wouldn't be allowed?

having trouble following your nested negative set logic sentence structure here.

Re: Computer Science Interview Questions with C++ Solutions

#12
post #9
post #2

Each question seems to come with just an uncommented code dump. Not any discussion of the actual algorithms, why it works, what the trade-offs are, etc. If I happened to ask all these questions in an interview and got all these answers, I would reject the candidate.

Moreover, I think that some answers are not doing what we should expect from the subject. For example the "Detect Multiple Of Two" detects powers of two, not multiples.

[deleted]

Re: Computer Science Interview Questions with C++ Solutions

#13
post #3

As a side note: never interview in C++. For these types of problems java is just so much easier to work with, and it's easy to learn enough java to interview with in a weekend from a C++ base. In my experience, interviewers always prefer a good easy solution (java) to a so-so difficult one (C++). Ruby and python are worth a try too, but often they make things so easy that the interviewer disregards the answer--many c…

I always tell candidates "I am multi-lingual, feel free to answer in any language you want, with bonus points for Lisp or Scheme."

So far no one has taken me up on the latter option. :(

Everyone ends up coding in straight C, rather annoying really. I'd kill for someone to pull out Python or some other language more suited to the problem.

All that said, straight Java is also likely a horrible choice. Boilerplate code sucks. If the interviewer allows it (and some are super strict about these things...) ask if it is OK to mix and match syntax. If someone wants to pull in C# lambda syntax along with making up their own multi-valued return syntax, fine.

Some problems are about demonstrating language mastery (especially for some positions!), other problems are about determining candidate problem solving abilities. For general problem solving ability questions, I'd prefer the candidate demonstrate the ability to think outside the bounds of any one language!

Re: Computer Science Interview Questions with C++ Solutions

#14
i don't know why but statistically the number of times i've written code to BFS on a binary-tree given the day-hour experience i've had, is zero.So why don't interview patterns change too.

If we expect the person to design,code,execute with you , why don't we check those aspects instead of pure-academic questions on integrity of a tree-search for high-performance solutions delivered in a 10-minute conversation.

Also may be am wrong & am the one with a green toe and rest of work-places do want only academicians with linked-list experience.

Re: Computer Science Interview Questions with C++ Solutions

#15

Earlier quoted context omitted.

Because neither is a language commonly used in interviews.

Maybe they're not commonly used in interviews because too few people refuse to take the sort of interviews where they wouldn't be allowed?

And that will continue to be the case, because the overwhelming majority of programmers even in really cutting-edge areas don't need it, so being able to speak Java is still valuable.

Knowing how to write code in functional languages has helped me write Java, too.

Re: Computer Science Interview Questions with C++ Solutions

#16
post #9
post #2

Each question seems to come with just an uncommented code dump. Not any discussion of the actual algorithms, why it works, what the trade-offs are, etc. If I happened to ask all these questions in an interview and got all these answers, I would reject the candidate.

Moreover, I think that some answers are not doing what we should expect from the subject. For example the "Detect Multiple Of Two" detects powers of two, not multiples.

Agree on needing comments.

My C++ is a bit rusty, but I think the code is in fact checking if the number is divisible by 2 (i.e. n % 2 == 0).

I think it's using the bitwise and operator (single &) to AND each bit in n and (n-1) and then checking if the least significant bit is 1 or 0. The code would need to shift (>) to check if the number were a power of 2.

I thought there was another bitwise NOT operator, not the !, but I think, and this is the part I'm hazy on after getting home, that the ! applied to an int is intended to flip each bit. Here's why:

------------

n = 6 = 110

n-1 = 5 = 101

n & (n-1) = 110 & 101 = 100

!(110) = 001 => true

------------

n = 5 = 101

n-1 = 100

n & (n-1) = 101 & 100 = 101

!(101) = 010 => false only if the least significant bit is used for logic decisions... this seems non-portable for some reason, just like using the ! as a bitwise NOT. It may actually be that ! only looks at the least significant bit, but I can't find c++ docs to say one way or another.

Whatever the code actually does, this is exactly the kind of example to use as a poster child for adding just a few comments.

Re: Computer Science Interview Questions with C++ Solutions

#17

Earlier quoted context omitted.

Maybe they're not commonly used in interviews because too few people refuse to take the sort of interviews where they wouldn't be allowed?

having trouble following your nested negative set logic sentence structure here.

Yeah, it reads a lot like that "Has Anyone Really Been Far Even as Decided to Use Even Go Want to do Look More Like?" thing.

Re: Computer Science Interview Questions with C++ Solutions

#18
post #3

As a side note: never interview in C++. For these types of problems java is just so much easier to work with, and it's easy to learn enough java to interview with in a weekend from a C++ base. In my experience, interviewers always prefer a good easy solution (java) to a so-so difficult one (C++). Ruby and python are worth a try too, but often they make things so easy that the interviewer disregards the answer--many c…

Eh, I don't know. Always interviewed with C++ with great results. Probably because I know the language and the standard library well enough to be able to write out the solution without looking into google or a textbook. ALWAYS use the language you're most comfortable with. If it's Java, let it be Java. If it's Python, let it be Python. Also, NEVER try to show off by programming your solution in some language you read about on the internet the day before because it's just asking for trouble.

Re: Computer Science Interview Questions with C++ Solutions

#19
post #3

As a side note: never interview in C++. For these types of problems java is just so much easier to work with, and it's easy to learn enough java to interview with in a weekend from a C++ base. In my experience, interviewers always prefer a good easy solution (java) to a so-so difficult one (C++). Ruby and python are worth a try too, but often they make things so easy that the interviewer disregards the answer--many c…

> Ruby and python are worth a try too, but often they make things so easy that the interviewer disregards the answer--many common string manipulation interview questions are one-liners in ruby. The problem is not that they are one-liners, the problem is that the algorithm is already implemented for you as a library function, and you just call it. OTOH, in Haskell a one-liner could perfectly contain the whole descript…

Write max flow in one line.

Re: Computer Science Interview Questions with C++ Solutions

#20
post #16
post #9

Earlier quoted context omitted.

Moreover, I think that some answers are not doing what we should expect from the subject. For example the "Detect Multiple Of Two" detects powers of two, not multiples.

Agree on needing comments. My C++ is a bit rusty, but I think the code is in fact checking if the number is divisible by 2 (i.e. n % 2 == 0). I think it's using the bitwise and operator (single &) to AND each bit in n and (n-1) and then checking if the least significant bit is 1 or 0. The code would need to shift ( >) to check if the number were a power of 2. I thought there was another bitwise NOT operator, not the…

The GP is correct, this is bit-twiddling code to test for a single bit being set in the int, and therefore being a power of 2, not simply a multiple of 2.

Broadly speaking, and ignoring edge cases, if N is a power of 2 then it has a single bit set. Subtracting one unsets that bit, so the AND of N and (N-1) is zero. On the other hand, if N is not a power of two then subtracting 1 leaves the top bit still set, so the AND is non-zero. Therefore taking the boolean NOT gives the right answer.

    00010000  Power of 2
    00001111  N-1
    --------
    00000000  -> 0

    00010110  Non power of 2
    00010101  N-1
    --------
    00010100  -> Non 0
Thus power of 2 is NOT( N & (N-1) ). Your examples are wrong because "!" is not bit-wise NOT, but boolean NOT. !0 = True, !N = False when N is non-zero.

It's actually wrong on the edge case of N=1, which is a power of 2.

Post reply on HN