Just goes to show... if you make up a question for beginners, you should write clear specifications. Otherwise you're going to have to argue with various people's interpretations of your unclear specifications.
Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.
I know. Just thought I'd inject a bit of education into the proceedings...
Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.
How about this one then?
From the passive-aggressive school of programming.
I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.
Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. Keeping my fingers crossed.
I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.
Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. I wonder if the interviewer is crazy. What purpose do questions like these serve ?
Keeping my fingers crossed.
I thought I would try it properly, so I went to my Java folder and tried to create a class: gedit ArrayPrinter.java. Only an old class appeared, dated “Oct 19 14:54”. It looks like this:-
java ArrayPrinter 1 2 69 4
{1, 2, 69, 4}
It prints args, which is a String[] rather than an int[], but the principles are the same. Work out what line 10 means. It prints {} if you pass an empty args array.
Andy Jack wrote:Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution.
Actually, Moe's solution also contains an inefficiency: it has to determine the result of array.length - 1 on each iteration. One alternative is:but my slight preference is:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.
Winston Gutkowski wrote:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.
Johan's solution won't throw an exception for an empty array.
Post by:autobot
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, that will fit in a stocking