- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
That's not a matter of belief. The magnitude of fibonacci numbers F(n) grows exponentially with n (it's almost ((sqrt(5)+1)/2)^2), therefore their length grows lineary with n. So, to add two numbers F(n) you need O(n) operations, and to multiply - about O(n*log(n)), as far as I know.
Admin
Thanks for bringing up the magnitude problem. I've been trying to explain that to the hash table guys on wikipedia (http://en.wikipedia.org/wiki/Talk:Hash_table#Look_up_is_not_O.281.29_at_all) : if your number of elements becomes large enough, you'll have to use a key of O(log(n)) bits, and its manipulation will take a time of O(log n), not O(1)
Admin
Admin
Reverse a string by swapping leftside with rightside.
String s = "ReverseMePlease"; int left = 0; int right = s.length() - 1; char[] c = s.toCharArray();
while(left < right) { char tmp = c[left]; c[left] = c[right]; c[right] = tmp; left++; right--; }
return new String(c);
Admin
Admin
Admin
I'm absolutely certain the last story had already appeared on this site before... I remember the "it's an idea floating around" comment.
Admin
Admin
Sorry everybody.
Admin
Admin
Admin
whoops
Admin
I think if you'd actually tried and execute this code, you'd understand it has at least one error.
Admin
And even if this code was right, this pinciple would give you O(n*log(n)) time for really large n (starting from 100 or so), at least if you wanted to calculate exact value of fibonacci number, not approximate.
Admin
As fake frits would say, "your[sic] not too bright, are you?"
Admin
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 ...several more 2147483647's ...
Admin
Admin
What, would you say, is the complexity of the fib function?
Admin
Admin
Admin
Admin
Admin
That is strange, since
gave me
0 1 2 8 24 80 256 832 2688 8704
Doesn't look like fibonacci sequence at all.
Admin
Converting a string from (7-bit) ASCII to (invariant) EBCDIC and back shouldn't result in any loss of information, as long as it doesn't contain any characters outside of the set 1234567890-=@%&*()_+qwertyuiopQWERTYUIOP|asdfghjkl;'ASDFGHJKL:"zxcvbnm,.ZXCVBNM<>? and regular space.
Admin
Admin
And if you can't understand recursion, you have no hope of understanding recursion.
(Sorry, I couldn't help it.)
Admin
However, I did say it quite explicitly.
Admin
What language are we supposing here? A Java string is a collection of chars, i.e. code points, not of bytes. So you don't need to worry about breaking the Unicode sequences when doing char manipulation.
Admin
But that calculates them in O(1). You need to add: sleep(log(n));
Admin
That's what happens when you fail to spend at least 2 hours studying algorithms.
Admin
Interesting...
Admin
I don't know whether surrogate pairs enter the picture here or not.
Admin
On a mildly serious note: To all those saying that the "right answer" is to use a built-in reverse function:
If I was applying for a job and the interviewer asked me to write a function to reverse a string, or if I was taking a class and this was a project assignment, I'd think an assumption in the question is that I can't just call a function that someone else wrote. I suppose I might ask if that's a legal solution. But presumably the point of the question is to test the appplicant's ability to write code to solve the problem, not his ability to look things up in the reference docs.
If you were given a school assignment to "write an e-mail program", do you think you could just hand in a copy of MS Outlook and get an A? (Well, probably a C, but that's another story.) That's not "writing an e-mail program", that's "making a copy of an e-mail program that someone else wrote".
I suppose it's possible that the goal of the question is to find out if you're familiar with the library, or if you know to use library functions rather than re-writing everything yourself from scrath. But if I was interviewing someone, "Does the applicant remember that a relatively-infrequently-used function is in the library?" does not seem like a useful thing to know.
Admin
You asked, implying it was the case, you didn't state it.
Admin
I know what you said. I gave you an (incorrect translation) of an algorithm that will do it faster than O(n). It's not O(log n), but it's faster than O(n). I thought it would be interesting.
The fibSeq() was part of the code used to frame the function.
The "whoops" was cause I mistyped a minus sign the first time. It still doesn't work. This does, as I actually tested it this time:
}
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 success
Admin
Admin
Don't be an ass. We already have plenty of those.
Admin
Admin
pseudocode typically doesn't involve references to specific class libraries does it?
You'll notices that I understood by "AppendFront(letter)", you meant "insert(0, letter)". I understand your pseudocode and that is it pseudo. But when we apply your approach it doesn't work out nearly as well as other approaches that have been posted, for the reason I gave.
You were wrong, get over it.
Admin
The best approach is to simply use a library function that exists to do the task at hand, and that is what I would do.
Admin
That's a REALLY backwards approach!
Admin
I saw "StringBuilder" and assumed java class libraries, but that's obviously not the case after a second glance. Then again, C#'s interface has the same method - underlying implementation, I don't know.
I agree about using library functions, but that's not what you did, then again, it's not what you're supposed to do on this test and the approach you took in doing what you were supposed to do wasn't ideal either, despite the fact that it's just pseudocode.
Admin
I still don't believe you've executed your code. All I get is 0 1 2 8 24 80 256 832 2688 8704 28160 91136 294912 954368 3088384 9994240 32342016 104660992 338690048 1096024064 3546808320 11477712896 37142659072 120196169728 388962975744 1258710630400 4073273163776 13181388849152 42655870353408 138037296103424 446698073620480 1445545331654657 4677882957791237 15137947242201104 48987426315567160 158526641599938752 513002988462146176 1660112543324047360 5372237040496678912 9223372036854775807 9223372036854775807 9223372036854775807 (repeatedly)
and then an exception.
Besides, I clearly see the error that leads to this error. But I don't understand, why would you lie you tested it?
Admin
Speak of the devil. Of course, you're a different kind of ass. boog is the kind that misunderstands things and then tries to make someone else look ridiculous so no one notices. You're the kind that takes jabs at others for being nerds despite the fact that you spend a good portion of your day here.
Admin
Admin
So you're under the impression that I got out a calculator and figured the output by hand?
Admin
Cool! We already forgot about how you (apparently) don't understand the complexity of algorithms. Nice work, boog!
Admin
I believe that either you post here not the code you execute, or you get the right output from some place else (maybe calculator, maybe almighty google). But again, your motives for this are unclear.
Admin
Re: answering the question in the article with a statement to use the library function: That's the first thing I would do, depending on how the question was worded. "How would you do this?" vs "Write this function". In the second case, I'd probably ask if we can insert a call to the function, which is something I've had to do at my job to "improve" a function that didn't exist in previous versions of the framework. Answering in this way shows that you actually know the framework and have used it. It's amazing when you interview people how many simply don't know how to use what they're working with.