magooly wrote: » Core Java Interview Questions(100) <snipped enormous list of questions>
schokea wrote: » Can you please post some of the whiteboard and SQL questions if you remember them
TonyStark wrote: » You are not permitted to use the IF construct.
ChRoMe wrote: » I hate those sort of bull**** questions.
schokea wrote: » Ye they are crap but I suppose every decent developer should know about the ternary operator: max = (a > b) ? a : b; but after reading this question I googled about a bit and found that the Java math library has a max() that does this for you: Math.max(10,20); returns 20
#include <stdio.h> unsigned char max(unsigned char a, unsigned char b) { unsigned char answer[65536]; int answerIndex = 0; for (int i = 0; i<=255; i++) { for (int j = 0; j<=255; j++) { /* this is setup, so using if here isn't * cheating because we could just enter * all this by hand... ewww */ answer[i<<8|j] = i > j ? i : j; } } /* here's the real max function */ answerIndex = a << 8 | b; return answer[answerIndex]; /* good programmers should now feel the need for a shower */ } int main () { printf ("max (128,255) = %i\n", max(128,255)); return(0); }
int diff = a - b; int complement = (diff >> 31) & 0x01; int max = a - k * diff;
Sparks wrote: » Here's the thing. That's a nice question if you want someone who can do low-level bit-twiddling in C or C++, but the whole point of languages like Java was to get the hell away from that kind of thing. So it's still a stupid Java question. You might as well ask someone to write an associative array in python from scratch instead of using dictionaries.
GreeBo wrote: » There are other approaches though, stick them into a sortable collection, sort and then pick the first (or last) element depending on the sort order for example.
I'd use the question to see if someone can come up with imaginative solutions that others can understand, not low-level crap that the next guy is going to have to google to understand before he can attempt to make a change.
Tar.Aldarion wrote: » It would take me a good while to even follow the logic in Sparks post
float InvSqrt(float x){ float xhalf = 0.5f * x; int i = *(int*)&x; // store floating-point bits in integer i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method x = *(float*)&i; // convert new bits into float x = x*(1.5f - xhalf*x*x); // One round of Newton's method return x; }
Sparks wrote: » So help me, if the crowd I was interviewing for accepted either of those answers, I'd have to think long and hard about working there. What exactly do you think the ternary operator and the max() functions are under the hood but if statements? So either they just mean don't use the if keyword in which case, that's a really horrible test and indicates a level of competence that hints to long nights fixing stupid mistakes; or they really mean don't use comparisons at all, which is a bitch of a question. First way I can think of if it's the latter demonstrates how ugly a question it is (I'll do it for chars here in C so it's more obvious): #include <stdio.h> unsigned char max(unsigned char a, unsigned char b) { unsigned char answer[65536]; int answerIndex = 0; for (int i = 0; i<=255; i++) { for (int j = 0; j<=255; j++) { /* this is setup, so using if here isn't * cheating because we could just enter * all this by hand... ewww */ answer[i<<8|j] = i > j ? i : j; } } /* here's the real max function */ answerIndex = a << 8 | b; return answer[answerIndex]; /* good programmers should now feel the need for a shower */ } int main () { printf ("max (128,255) = %i\n", max(128,255)); return(0); } Nasty. In my defence, it's late and I'm tired If I was running a tech interview and someone came up with that answer to that question but said it was messy, I'd pass them because interviews are stressful, but I'd send it back if it showed up in a code review in day-to-day work as being too mucky. There is a much, much cleaner way if you bittwiddle, because signed ints will be in two's complement format on pretty much every machine I can think of so you can check the sign without doing comparisons something like this: int diff = a - b; int complement = (diff >> 31) & 0x01; int max = a - k * diff; So you're grabbing the two's complement bit of diff with the shifting there (we're assuming 32 bit ints, but that's a reasonably safe bet in modern C/C++) (and really the masking shouldn't be needed, I just have really old habits ), and it'll be either 0 or 1 so k*diff will be 0 if a > b and it'll be equal to a - b if a < b; therefore max is either just a (if a > b) or a - (a - b) which is just b, if a < b. Here's the thing. That's a nice question if you want someone who can do low-level bit-twiddling in C or C++, but the whole point of languages like Java was to get the hell away from that kind of thing. So it's still a stupid Java question. You might as well ask someone to write an associative array in python from scratch instead of using dictionaries.
public int getMaxWithNoIFWhile(int a, int b) { while(a>b) { return a; } return b; } public int getMaxWithNoIFFor(int a, int b) { for(int i;a>b;i++) { return a; } return b; }
Sparks wrote: » Yeah, that's still cheating, you're still using conditionals hidden in the while and for loops
Sparks wrote: » No it wouldn't, if you weren't at work and weren't distracted, you wouldn't even get through a cup of coffee in the time it took to grok it. You want something hard to follow at that level, go look up John Caramack's inverse square estimation routine in the original Quake engine code: float InvSqrt(float x){ float xhalf = 0.5f * x; int i = *(int*)&x; // store floating-point bits in integer i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method x = *(float*)&i; // convert new bits into float x = x*(1.5f - xhalf*x*x); // One round of Newton's method return x; } There's an entire academic paper written about that one (Well, okay, the paper is mainly about the choice of 0x5f3759d5 as the magic number in there, but it's still shiny).
Tar.Aldarion wrote: » FBs newest employee
Distracted is right, all of our products now echo my name to the screen temporarily
hmm another one I was asked was convert string to int without using atoi etc, get cracking lads.
Tar.Aldarion wrote: » hmm another one I was asked was convert string to int without using atoi etc, get cracking lads.
digits = "0123456789" result = 0 for d in input_string: result = result * 10 + digits.index(d)
oscarBravo wrote: » less... elegant languages.
Tar.Aldarion wrote: » I went for an android interview, 3rd and final round. The whole test was in C/++ and was got to do with "do this without using inbuilt functions and bonus marks for the more shifting of bits you do". At no stage in the interviews was anything java or android really asked about, an odd question here and there. It was disgusting, and they were offering 20k. The whole point is that I'm abstracted from that stuff, waste of time, and I had no idea how to do anything with bit shifting anyway. It would take me a good while to even follow the logic in Sparks post
Sparks wrote: » I love me some Python, but even I wouldn't have said that while Ruby was listening
oscarBravo wrote: » I can't get to love Ruby. The syntax just leaves me cold. After almost three decades of professional software development, I think I've found my One True Love in Python.
GrumPy wrote: » Third round, they gave you a C++ exam (for Android?!), and offering 20k? Jaysus! That's an insult. 3 interviews deep, and an exam that doesn't really make sense.
oscarBravo wrote: » In Python: digits = "0123456789" result = 0 for d in input_string: result = result * 10 + digits.index(d) Adapt as necessary for less... elegant languages.
a - ((a-b)*((a-b)>>31))
Sparks wrote: » Yeah, the top answer there is doing what I was doing (exactly the same thing in fact), but I'll happily lay claim to have written the more readable version! (Not to mention, any decent compiler would take my code and his code and produce the same assembler - the only difference is that I'm using an explicit intermediate variable and doing the steps on different lines).
int diff = a - b; int complement = diff >> 31; int max = a - (diff & complement);
everdead.ie wrote: » Hey I know these threads come up quite frequently basically I have a tecnical test with IBM without any real idea what to expect. To be honest what has made it worse is that the manager I did the first interview laughed at how easy it was and assured me if be fine, then the HR lady said it was never something anyone complained about but I really have no idea where to review/practice and now I'm worried I'll make an eijet of myself. So I thought a thread on technical questions might come in handy for people to practice before going into interviews. So no answers unless someone is stuck but previous questions I have encountered are. Q1 How to tell if a binary is a palindrom. Q2 Given two lists List all the unique words which occur in each list and if a word occurs multiple times show it multiple times. (This was badly phrased when I got it so I'm going to show some examples to explain) L1=Tom,Tom,Tom,John L2=Tom,john, mike,Rob Combined list = Tom,Tom, Mike, Rob Also any advice for my own test let me know.