Re: Had an interview



Robert Adsett wrote:
However, the fact that no one considered that the inputs could be out of range (and for that matter your reaction to the observation) is an interesting commentary on how we view this practice we call programming. We seem to default to the assumption that the input values to our current problem will be in the range we expect.

Absolutely not. The correct view of the situation is that the notion of "verifying the input" is not generally applicable to and abstract interview-level problem, aside from the questions specifically crafted for the purpose of testing the subject's ability to perform the verification.

There are different approaches to verifying the input and each one is applicable in its own set of situations. One can use a permanent run-time check, a debug-only assertion, something else or not check input at all. The key moment here is that the major part of the art of programming is to develop solutions that solve specific, concrete problems by applying generic, abstract algorithms.

For example, given a interview problem of sorting an array of integers, filled with only even values, one might decide to use some known generic sorting algorithm (say, quicksort). But should one check the input first and make sure that there are no odd numbers in the array? When the solution does not rely on that, the answer is - no! The value of generic sort solution in this case is that it not only solves the original problem, but also applies to the whole variety of inputs (i.e. any input).

The same logic applies in this case. A solution that depends on the input to be in range is a B-grade solution. A solution that does not depends on the input to be in range, but still verifies it is a C-grade solution. An A-grade solution would behave correctly on the specified input, but also do something similar (and theoretically "useful") on the other inputs (meaning that there no need to verify the input). This is actually the main reason why the 'x = 3 - x' is the best solution of all presented. It is readable and at the same time it is based on the cleverly noticed elegant mathematical generalization. Adding an input check would make it as ugly as the 'if'-based solution. No, the input check is the responsibility of the caller in this case.

--
Best regards,
Andrey Tarasevich
.



Relevant Pages

  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily ... design, PIC code, and real time programming. ...
    (rec.pets.dogs.behavior)
  • Re: LISPPA
    ... > The abstract mathematical definition doesn't cope too well ... programming languages which do not allow to write bad programs, ... definition of array? ...
    (comp.lang.lisp)
  • Re: How do i change numerics into binary numbers?
    ... my first time trying out on programming. ... how do I extract the output from the user to make it an array? ... Those are needed details. ... what you expect for input, (even if you know that it may be garbage, you have ...
    (microsoft.public.vb.general.discussion)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... Maybe this is true in the sort of programming you do, ... you are storing a list of amounts of money as integers. ... int average ... the array, rounded towards zero. ...
    (comp.lang.c)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...   and improve your crack pot licensing fee calculator. ...
    (rec.pets.dogs.behavior)