11

I am 2nd-year computer science student and I just got back from my first ever interview.

Basically at the end my interviewer asked me to write a function that takes two arguments, an amount in pounds and a tax percentage and then return an array with the tax amount.

I eventually wrote something like this after some trial and error:

public static double[] taxAmount (double pounds, double taxPercentage) {
    double taxAmount = pounds * taxPercentage/100;
    double[] taxAmountArray = new double[1];
    taxAmountArray[0] = taxAmount;
    return taxAmountArray;
}

It worked and he seemed happy, what I am wondering is why I needed to return an array for this task? I just feel like the question was really stupid the array is useless for this task right?. Am I missing something?

5
  • 6
    FYI: Code could have been as simple as return new double[] { pounds * taxPercentage/100 };
    – Andreas
    Commented Dec 11, 2017 at 6:28
  • @Andreas I wanted to do something like that but I couldn't remember the syntax for initialising an array with values in one line. I had to do the task in notepad for some reason as well which was strange. Commented Dec 11, 2017 at 6:31
  • 4
    @deadcode I'm not sure how applicable your comment is to Java. Java doesn't have explicit pointers. Commented Dec 11, 2017 at 7:01
  • 1
    FYI, You probably should have also asked about why a monetary value is being represented as a double instead of a BigDecimal. Double vs. BigDecimal?
    – martin
    Commented Dec 11, 2017 at 8:19
  • @martin, yes, the obvious wrong with returning an array for a single value at least has no consequences to the correctness of the program, while FP errors and money could be much worse
    – fede s.
    Commented Dec 11, 2017 at 8:55

2 Answers 2

21

The interviewer most likely wanted you to comment on why you were being asked to return the tax amount in an array, just like you are doing right now.

If you voiced your confusion during the interview like you did in this question, you passed the test.

Essentially the question was likely designed not just to check if you could identify the bizarreness of returning an array, but also whether you would have the confidence to be able to communicate your confusion and challenge your future supervisor if you got the job.

3
  • 4
    That makes so much sense... I think I did. I asked "What is the point of the array" at the end. Commented Dec 11, 2017 at 6:13
  • 3
    And what did he say ? Irrelevant, I know, I am just curious.
    – Kanmani
    Commented Dec 11, 2017 at 6:36
  • 1
    That was my exact thought when reading the title...
    – RonJohn
    Commented Dec 11, 2017 at 8:36
1

The interviewer probably wanted you to interact more so that he could see how you think, and approach the problem and for this particular instance he probably wanted you to ask why you should return an array.

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