Comment On Three for Three, Recursion Threads, and Wrong Answer

Three for Three (from Randall C) My boss, Harry, needed to hire some experienced software engineers, so he placed a job ad and did some kind of sort on the fifty or so replies that came in. Later, many of us assumed that he must have done a stair sort (throw them from the top of a stair and pick the ones that traveled farthest). He then said to me "I need to hire two engineers and I've selected three candidates. Would you like to interview them?" Not actually stated in this, but his attitude clearly indicated that he planned to hire two engineers from that pool of three. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 11:51 • by woliveirajr (unregistered)
frist ! with aliquam

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 11:53 • by Mason Wheeler
Re: Wrong Answer, this sounds a lot like the original Inner-Platform Effect story. Same company?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 11:57 • by CaptainCaveman
"And the only reason that release went out was they had fired the QA group for repeatedly rejecting Charles' and Terry's high quality work."

Hooray, lets fire people for doing their jobs well. I have seen this shit myself in the not-so-distant past. People who don't work in IT wonder why I became so jaded.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 11:59 • by Anon (unregistered)
354201 in reply to 354200
CaptainCaveman:
"And the only reason that release went out was they had fired the QA group for repeatedly rejecting Charles' and Terry's high quality work."

Hooray, lets fire people for doing their jobs well. I have seen this shit myself in the not-so-distant past. People who don't work in IT wonder why I became so jaded.


And I'm sure they were told that they were being fired because they aren't "team players".

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:00 • by Not a Java programmer (unregistered)
To clarify, that second one just reverses the string right? Does the Java string type not have a method to do that?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:01 • by John Galt (unregistered)
Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:03 • by John Galt (unregistered)
354204 in reply to 354203
John Galt:
Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.


Ah - never mind, didn't see the parenthesis in the last line.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:04 • by dohpaz42
354205 in reply to 354202
Not a Java programmer:
To clarify, that second one just reverses the string right? Does the Java string type not have a method to do that?


Yes, it reverses the string. And, it doesn't matter if there is a built-in for that; this was a coding challenge to gauge the ability of the interviewee to see if they could tell from looking at the code what the code should be doing - well, I hope that's what it is, anyway. :)

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:05 • by Xenon Xavior (unregistered)
354206 in reply to 354203
John Galt:
Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.


That's what I thought at first, but look closer at the parenthesis. It will reverse the string.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:05 • by hoodaticus
I once interviewed a "C++ programmer" who couldn't pass FizzBuzz. He didn't even know how to do logical negation (!).

Less amusing, I had an assembly programmer who did pass it, but without using the mod operator or even attempting to roll his own. He had to use string conversion and tested for the decimal point.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:06 • by Not a Java programmer (unregistered)
354208 in reply to 354205
I looked it up and it doesn't anyway.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:06 • by hoodaticus
354209 in reply to 354203
John Galt:
Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.
Is this the John Galt from Stack Overflow?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:09 • by bob (unregistered)
354210 in reply to 354208
"I looked it up and it doesn't anyway"

Not suprising, when would that ever come in useful for anything useful?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:12 • by WC (unregistered)
354211 in reply to 354209
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:12 • by bencoder (unregistered)
354212 in reply to 354210
bob:
"I looked it up and it doesn't anyway"

Not suprising, when would that ever come in useful for anything useful?


Have you seen Java?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:14 • by dohpaz42
354213 in reply to 354211
WC:
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.


Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:17 • by resmoker10 (unregistered)
354215 in reply to 354210
Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:19 • by snoofle
We're stuck on 1.5, so:

return s==null || s.length()<2 ? s : new StringBuilder(s).reverse().toString();

Though I once got: what if you couldn't use StringBuilder?

I didn't want to work for the guy so I said I'd hire a junior programmer to handle the piddle work so I could get on with solving the company's real problems.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:19 • by dohpaz42
354217 in reply to 354215
resmoker10:
Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.


I hope that you don't mean that you do String.encrypt(String.reverse(String.reverse)), and I'm just reading your post wrong!

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:20 • by Helix
354218 in reply to 354207
hoodaticus:
I once interviewed a "C++ programmer" who couldn't pass FizzBuzz. He didn't even know how to do logical negation (!).

Less amusing, I had an assembly programmer who did pass it, but without using the mod operator or even attempting to roll his own. He had to use string conversion and tested for the decimal point.


I am trying to think how you would do fizzbuzz with strings and chars in asm?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:20 • by boog
354219 in reply to 354199
Mason Wheeler:
Re: Wrong Answer, this sounds a lot like the original Inner-Platform Effect story. Same company?
Sadly, I doubt it. This anti-pattern is all too frequent in IT, as so many developers see it as a fun and challenging way to avoid the boring task of thinking about data structures.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:22 • by snoofle
354220 in reply to 354217
dohpaz42:
resmoker10:
Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.


I hope that you don't mean that you do String.encrypt(String.reverse(String.reverse)), and I'm just reading your post wrong!
That's exactly what he meant - you missed the implied "</wink>"

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:23 • by somechick (unregistered)
354221 in reply to 354207
hoodaticus:
I once interviewed a "C++ programmer" who couldn't pass FizzBuzz. He didn't even know how to do logical negation (!).


I see what you did there ...

I still say my favorite interview question I was ever asked was "What is this '|' symbol?" 'A Pipe' "What is it used for?" 'It's a pipe.' My brain went blank for a couple seconds there and then I explained what pipes do on the unix command line (in more detail than was necessary, from what my boss said afterwards, as they weren't looking for the term FIFO, oddly enough).

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:27 • by NickS (unregistered)
Oh, that idea's floating around? Here's what you should do: Shoot it down, take it outside, put it on the hood of whoever thought it up, light it on fire, and leave it as a warning to others who attempt to have similar "ideas".

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:31 • by piskvorr
Re Wrong Answer: I think I used to work there. Running away, screaming in terror, would indeed be the sane course of action - the founder used to program, way back in the 1990s, and most of such brillant inventions were his. (And yes, you were right, that inner platform turned into a Lovecraftian nightmare no-one dared disturb)

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:32 • by Anon (unregistered)
Other than this being a reverse function, since I am not a java developer what *would* be the proper way to reverse a string since the interview question explicitly asks how you would improve on the function.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:36 • by Rootbeer

How come there isn't a caffeinated soft drink called FizzBuzz?

Maybe the name is TOO clever. Self-identifying geeks are content to suck down a half-dozen Bawls per day.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:39 • by Machtyn (unregistered)
354228 in reply to 354217
dohpaz42:
resmoker10:
Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.


I hope that you don't mean that you do String.encrypt(String.reverse(String.reverse)), and I'm just reading your post wrong!


Actually, I think he means String.reverse(String.reverse). He didn't mention anything about an encrypt in "Password reversal is a useful when storing passwords..."

String.reverse(String.reverse("I then had a good laugh because they are now double encrypted")). (They'll never guess what I said now!)

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:50 • by Abso (unregistered)
354231 in reply to 354224
Anon:
Other than this being a reverse function, since I am not a java developer what *would* be the proper way to reverse a string since the interview question explicitly asks how you would improve on the function.

I'm not a java developer either, but I suspect the answer is to use a loop instead of recursion.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:51 • by ooblek
I'm currently working on an "enterprise" application with such a meta-database.

I just had to write a query that had a dozen table joins in it. The join criteria is a bunch of strings. It is slow as hell, but there is nothing I can do about it.

At least a few of the joins are on the same table with different criteria. They store the relations in a single column of a table. A lot of the tables have columns with names like ATTRIB_001 up to ATTRIB_100+.

Fortunately, I don't have to work at this low of a level in this application much.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:52 • by Hanoi 4 ever (unregistered)
354233 in reply to 354213
dohpaz42:
WC:
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.


Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).


Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:53 • by Joel B
354234 in reply to 354228
I think you forgot to run ROT13 on that. Twice.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:55 • by hoodaticus
354235 in reply to 354218
Helix:
hoodaticus:
I once interviewed a "C++ programmer" who couldn't pass FizzBuzz. He didn't even know how to do logical negation (!).

Less amusing, I had an assembly programmer who did pass it, but without using the mod operator or even attempting to roll his own. He had to use string conversion and tested for the decimal point.


I am trying to think how you would do fizzbuzz with strings and chars in asm?
It wasn't a machine coder's position. He was using .NET.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 12:56 • by Anon (unregistered)
354236 in reply to 354215
"storing passwords in a database".
And that's TRWTF.

Reversing them won't help you. Specially when the hackers begin eikooc as a password.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:00 • by Anon (unregistered)
354239 in reply to 354233
Hanoi 4 ever:
dohpaz42:
WC:
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.


Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).


Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.


Or calculating factorial. I always thought that was the canonical example.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:00 • by Sutherlands
354240 in reply to 354224
Anon:
Other than this being a reverse function, since I am not a java developer what *would* be the proper way to reverse a string since the interview question explicitly asks how you would improve on the function.

Psuedocode:

foreach(letter in string)
StringBuilder.AppendFront(letter)

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:01 • by Jeff (unregistered)
What about improvements to the recursive string reversal code?

I'm thinking:

1) First choice, use any built in library functions since they're already coded, tested, and optimized.

2) Don't use recursion since that's a lot of overhead and you could get a possible stack overflow if the string was long. (Can you even get stack overflows in C#? I honestly haven't had one in many many years).

3) As a coding exercise, off the top of my head I'd probably use a one character buffer and swap characters from the outside to the inside. Ala:

strToRev="CodeTest";

for(int i=0; i< strToRev.length() / 2; i++) {
int farCharPosition = strToRev.length()-i-1;
char buf = strToRev[farCharPosition];
strToRev[farCharPosition] = strToRev[i];
strToRev[i] = buf;
}

Thoughts?

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:02 • by C-Octothorpe
354242 in reply to 354231
Abso:
Anon:
Other than this being a reverse function, since I am not a java developer what *would* be the proper way to reverse a string since the interview question explicitly asks how you would improve on the function.

I'm not a java developer either, but I suspect the answer is to use a loop instead of recursion.
That's what I would've done.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:02 • by Abso (unregistered)
354243 in reply to 354233
Hanoi 4 ever:
dohpaz42:
WC:
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.


Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).


Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.


This wasn't meant as an example, though. It makes sense for them to use something that would be easier to do with a loop, because then they can look for people whose answer to the second question is "use a loop".

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:09 • by Hortical (unregistered)
354244 in reply to 354240
Sutherlands:

Psuedocode:

foreach(letter in string)
StringBuilder.AppendFront(letter)


Consider the possibility that the StringBuilder uses a char[] internally.

Consider the possibility that every time you insert a char at the beginning, all the other chars that have been added have to be moved over a space. Every time.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:12 • by Dank (unregistered)
354246 in reply to 354236
Anon:
"storing passwords in a database".
And that's TRWTF.

Reversing them won't help you. Specially when the hackers begin eikooc as a password.


Failing to comprehend sarcasm. That's TRWTF. Unfortunately this won't help you. Specially when the jokers begin to mock you too.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:14 • by TheCPUWizard
EAV (Entity, Attribute Value) data structures have real uses in many application, unfortunately they seem to be abused much more often than used properly.

There are a complete set of transparent optimizations that can be done in this environment, that are much harder to implement with table/column fixed formats.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:15 • by resmoker10 (unregistered)
354250 in reply to 354246
no the RWTF is this comment in which I comment on the fact that your comment was the RWTF

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:15 • by Jay (unregistered)
There are basically two reasons why someone might ask your opinion.

1. On rare occasions, it may be because the asker has not made up his mind and believes that insights or suggestions that you have may help him to arrive at a better decision.

2. Most of the time, the asker has already made up his mind and is looking for someone else to validate his decision.

I regularly have conversations with my boss where he asks, "What do you think we should do about X?" Then I say what I think we should do. Then he explains to me why he's already decided to do something else.

Still, it's better then the way we used to work. That was, he would make a decision about something. Then he would tell me to come up with a solution to whatever problem, without telling me what his idea was. I would spend days writing up a design document or coding a program or whatever. Then he would yell at me because I hadn't done it the way he wanted. I thought of it as "the I'm-thinking-of-a-number management style".

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:20 • by Dank (unregistered)
354252 in reply to 354246
String reverseString(String input) {
if (input == null || input.length < 2) return input;

char[] inp = input.toCharArray();
StringBuilder output = new StringBuilder();

for (int i = inp.length - 1; i >=0 i--) {
output.append(inp[i]);
}
return inp.toString();
}

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:27 • by Hortical (unregistered)
354255 in reply to 354252
Dank:
String reverseString(String input) {
if (input == null || input.length < 2) return input;

char[] inp = input.toCharArray();
StringBuilder output = new StringBuilder();

for (int i = inp.length - 1; i >=0 i--) {
output.append(inp[i]);
}
return inp.toString();
}


You should have read all the comments.
Someone already did this better than you.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:31 • by dohpaz42
354258 in reply to 354233
Hanoi 4 ever:
dohpaz42:
WC:
No, it's the John Galt from Atlas Shrugged.

I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.


Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).


Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.


Recursion is usually a precursor to larger algorithms, such as sorting. If you can't understand recursion, you have no hope of quick sort or tree traversal.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:31 • by hoodaticus
354259 in reply to 354215
resmoker10:
More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.
+5

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:34 • by C-Octothorpe
354262 in reply to 354259
hoodaticus:
resmoker10:
More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.
+5
Here, here! They should add this to the OWASP secure development guide.

Re: Three for Three, Recursion Threads, and Wrong Answer

2011-07-21 13:37 • by wizzard
I have encountered "meta-databases" at least four times in my career. I will never understand why everybody thinks they're such a wonderful, revolutionary idea. I know there are some legitimate applications for EAV but these are not them.

One project I worked on had to do so many JOINs just to get a single record out of their database that they exceeded MySQL's JOIN limit (61). Twice. They had to write code to keep track of the # of joins while building the query, and break the query into separate pieces when necessary. It goes without saying that the queries also took about 100 times longer than necessary, and data integrity was a complete nightmare.
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment