Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am using one of the practice online judges where a practice problem is asked and one submits the answer and gets back if it is accepted or not based on test inputs.

My question is the following:

In one of the practice tests, I needed to sort an array as part of the solution algorithm.
If it matters the problem was: find 2 numbers in an array that add up to a specific target.
As part of my algorithm I sorted the array, but to do that I used Java's quicksort and not implement sorting as part of the same method.

To do that I had to do:
java.util.Arrays.sort(array);

Since I had to use the fully qualified name I am wondering if this is a kind of "cheating". (I mean perhaps an online judge does not expect this)
Is it? In a formal interview (since these tests are practice for interview as I understand) would this be acceptable?

share|improve this question

3 Answers 3

Carrying the "cheating" logic to extremes would mean you'd also have to avoid anything that isn't a language primitive (i.e., code someone else wrote). So no sorting, no collections or anything else you have to import. But that seems like overkill, doesn't it?

If it were me and I hadn't specifically said "write a sort," you'd get dinged for not using every avenue available to minimize the amount of code that has to be developed and maintained. Most interviewers want to see what you'd be doing if you were working for them. Unless you're interviewing for a position rewriting sort algorithms, using the canned version shows that you're familiar with all the pointy things in your tool box.

One major difference between an on-line test site and a real interview is that you can ask questions such as "I'm going to use a sort in solving this problem. Any objection if I use the one out of the library?"

share|improve this answer

In a formal interview (since these tests are practice for interview as I understand) would this be acceptable?

If there are any unspoken rules about what is and isn't acceptable in a code question at an interview, they have more to do with respecting the other people in the room and communicating clearly than using or not using library functions:

  • Listen carefully.
  • Don't waste time.
  • Answer the question as clearly as you can.
  • If you don't understand the question or need more information, ask.

If someone asks you to print some output, are you going to write the print function from scratch? Of course not.

If someone asks you to solve a problem not directly related to sorting, and if sorting the input helps, do you have to write the sort function from scratch? Surely not.

If you provide an answer using the standard sort function, what's the worst that could happen? Any sane interviewer who wants to know if you can write a sort should ask a follow-up question like: "Great, but let's say that for some reason you're prevented from using java.util -- could you show us how you'd write that sort function yourself?" At that point, the question is about knowing how to implement a sort algorithm, and using some external sort implementation would be a poor answer.

share|improve this answer
    
+1:Does the fact that the practice tests expect the fully qualified name give a hint that the build in function is not to be used in practice test? –  user10326 Nov 19 '11 at 14:52
    
I have no idea what practice tests you're using or what the convention is there, but my feeling would be no, you should use whatever tools you'd normally use to write code. More important: why do you care? Use practice tests or other forms of study in whatever way works best for you. You seem pretty concerned about the use of sort(); if you don't know how to implement common sorts like quicksort you might want to practice that. But if there aren't any stated rules to the contrary, and if you're doing this on your own terms, use whatever you like. –  Caleb Nov 19 '11 at 15:01

Seeing as the intent of such tests (at interviews at any rate) is to see if you can write out the algorithm, using a built in sort will give no indication about your abilities.

I wouldn't call this cheating, but at the same time, you know that's not what they are looking for and are simply shooting yourself in the foot.

If this was in a competition, and the competition rules didn't specifically exclude using library function, I wouldn't call that cheating - I would call that good use of your resources.

share|improve this answer
    
Oded: I assume you are talking about a formal interview?Doesn't the fact that I know that there is already a sorting algorithm available as part of the Java library and know that it is specifically implementation of quicksort show anything you mean? –  user10326 Nov 19 '11 at 11:00
2  
@user10326 - It shows that you can read the documentation. Again, it is about the intent of the interview. If the idea is to implement quicksort yourself, then you are not helping yourself. –  Oded Nov 19 '11 at 11:03
    
+1 since this is sensible.I just don't see why would someone expect from me to reinvent the wheel –  user10326 Nov 19 '11 at 11:06
    
@user10326 - It depends on the kind of work expected from you. If it is low level enough, knowing about algorithms and how to implement them may very well be important to the job at hand. If you were to work on LOB software, I would rather see you use the built in libraries. –  Oded Nov 19 '11 at 11:10
1  
The question in this case relates to finding a specific pair of numbers, not sorting. Failing to use the standard sort routine could easily make it look like you don't know what you're doing. –  Caleb Nov 19 '11 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.