Re: Had an interview
- From: Andrey Tarasevich <andreytarasevich@xxxxxxxxxxx>
- Date: Tue, 06 May 2008 14:32:39 -0700
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
.
- Follow-Ups:
- Re: Had an interview
- From: Robert Adsett
- Re: Had an interview
- References:
- Re: Had an interview
- From: Andrey Tarasevich
- Re: Had an interview
- From: Spehro Pefhany
- Re: Had an interview
- From: Robert Adsett
- Re: Had an interview
- From: Andrew Smallshaw
- Re: Had an interview
- From: Robert Adsett
- Re: Had an interview
- Prev by Date: Re: ASCII Text to I8HEX Format Converter
- Next by Date: Re: Loss of synchronisation: Are the chips reliable?
- Previous by thread: Re: Had an interview
- Next by thread: Re: Had an interview
- Index(es):
Relevant Pages
|