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?

  • 6
    *FYI:* Code could have been as simple as `return new double[] { pounds * taxPercentage/100 };` – Andreas Dec 11 '17 at 06: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. – StormzyInnit99 Dec 11 '17 at 06:31
  • 4
    @deadcode I'm not sure how applicable your comment is to Java. Java doesn't have explicit pointers. – Bernhard Barker Dec 11 '17 at 07: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?](https://stackoverflow.com/questions/3413448/double-vs-bigdecimal) – martin Dec 11 '17 at 08: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. Dec 11 '17 at 08:55

2 Answers2

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.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
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.

Samy
  • 116
  • 1
  • 4