Interview questions gone wrong...
-
The interviewer meant POWER of 2 ie..1,2,4,8,16,32 and i believe, (((x&(x-1))+1)==2*x) is the right answer.... coz any power of 2 will be of the form 00001000.... and x-1 will of form 00000111.... Now when we do x&(x-1)...ie. bitwise and we get 00001000... 00000111... ------------ 00001111... whichh is 1 less than 0001000 ...(x*2)
AmitDey wrote:
Now when we do x&(x-1)...ie. bitwise and we get 00001000... 00000111... ------------ 00001111...
:confused: This "&" is the bitwise operator for the logical function AND, so, with it you would get 00001000... 00000111... ----------- with operator & 00000000... What you need is the bitwise operator for the logical function OR. The operator is "|" so this operator would be the correct one, hence the expression is "x|(x-1)" 00001000... 00000111... ----------- with operator | 00001111...
-
The interviewer meant POWER of 2 ie..1,2,4,8,16,32 and i believe, (((x&(x-1))+1)==2*x) is the right answer.... coz any power of 2 will be of the form 00001000.... and x-1 will of form 00000111.... Now when we do x&(x-1)...ie. bitwise and we get 00001000... 00000111... ------------ 00001111... whichh is 1 less than 0001000 ...(x*2)
I think you meant x|(x-1)... But there is another way: x&(~(x-1))==x 00001000... = x 00000111... = x-1 11111000... = ~(x-1) 00001000... = x&(~(x-1)) This should work even if x = 2 (...0000001) or x = (1000000...) Mikon Dosogne
-
I think you meant x|(x-1)... But there is another way: x&(~(x-1))==x 00001000... = x 00000111... = x-1 11111000... = ~(x-1) 00001000... = x&(~(x-1)) This should work even if x = 2 (...0000001) or x = (1000000...) Mikon Dosogne
-
Even better! But did you google the answer? ;)
-
I agree that those types of questions are probably more about the interviewer's ego than finding the skill level of the applicant. But, that question made me think, a little.:doh: Consider this. All powers of 2, in binary are in the following form: 01, 10, 100, 1000, 10000 etc... which of course is 1, 2, 4, 8, 16, etc... So the easy way to tell is just any number that in binary is a single 1 followed by all zeros.
sum all binary and result should be 1 eg : 8= 1000 (bin) 1+0+0+0 =1 // convert binary to string replace '0' with '' check if string length is 1 // ;) and you can explain them this is the most optimesed way it can be cause all the interviwer are not techies they are mostly from hr dept with some questoins (limited) and even if they belong programming they dont prove you wrong so you are right ;) -- modified at 13:24 Tuesday 19th September, 2006
-
Even better! But did you google the answer? ;)
-
Had an interview over the phone today. Totally bunged the question. They asked for a test to check if a number is a power of 2 and I answered it as a test to see if it was divisible by 2! Should have been (x && !(x & (x - 1))). What a dolt I be! I was so nervous I messed up other simple ones too. Needless to say I didn't get the job failing the technical questions. Anyone have other mixed up interview questions?
Jeremy Pemberton-Pigott
A programmer with a dream can accomplish anything. So, start by implementing your castle in the clouds and then working on its interface to a foundation :-D Quote by: Jeremy Pemberton-Pigott
New Dawn EngineeringI was asked in an interview to write the algorithm to find n numbers in the Fibonacci series, or whatever. I misunderstood them so I accidentally wrote a program that printed on the screen a large phallic figure, and to decode the words corresponding to the acronym "YUCK FOU", and quickly left.
-
Right bracket, x, logical and, not, right bracket, x, binary and, right bracet, x - 1, left bracket, left bracket, left bracket ;)
Pablo Sometimes I think there's no reason to get out of bed . . . then I feel wet, and I realize there is.
-
Polymorpher wrote:
Pablo Sometimes I think there's no reason to get out of bed . . . then I feel wet, and I realize there is.
:laugh::omg::wtf:
We made the buttons on the screen look so good you'll want to lick them. Steve Jobs
LOL, I found that on a homer simpson sight.;P
Pablo Sometimes I think there's no reason to get out of bed . . . then I feel wet, and I realize there is.
-
sum all binary and result should be 1 eg : 8= 1000 (bin) 1+0+0+0 =1 // convert binary to string replace '0' with '' check if string length is 1 // ;) and you can explain them this is the most optimesed way it can be cause all the interviwer are not techies they are mostly from hr dept with some questoins (limited) and even if they belong programming they dont prove you wrong so you are right ;) -- modified at 13:24 Tuesday 19th September, 2006
Dim Number As Integer = ??? Dim Temp As Integer = 1 Do Until Temp > Number Temp <<= 1 Loop While Temp > 0 If Number < Temp Then Return False Else Number -= Temp End If Temp >>= 1 End While Return True ...Unless there is a simpler way to convert an integer to binary I think the original way was faster
Pablo Sometimes I think there's no reason to get out of bed . . . then I feel wet, and I realize there is.