Unexpected response to a professional interview

This is a 100% theoretical question and, possibly, an opinion is based.

In a professional interview, I got a printed page with a lot of badly written and incorrectly formatted code for two class es to analyze them line by line in speech. Let me name these data structures A and B There was no problem or any information about the expected behavior.

However, one of their questions really pissed me off.

After identifying the suspicious behavior of the algorithm and many potential and actual errors, they asked me to identify another error. I identified several other obvious problems, but I did not understand what they want from me. Well, when they told me the answer, it blew my mind. A simplified version of A and B follows (I left only those methods that are part of their idea):

 template <typename T> class A { public: // elements are dynamically allocated on the heap const T& getValue(unsigned i) const // I'm not even sure it was const { // return element at position 'i' } virtual void setValue(unsigned i, const T &value) { // sets the element at position 'i' } }; template <typename T> class B: public A<T> { public: virtual void setValue(unsigned i, const T &value) { // set the element at position 'i' using A<T>::setValue() // then sort all elements in descending order } }; 

Well, the solution is not a compilation, runtime, or logical error. Error: since B::setValue sorts the data after the item is placed, you cannot check if the data that you receive at this position is available is the same as what you saved at this given position. However, with A you can do this. This unsurpassed behavior between A and B is considered a mistake (and they added that this is not a mistake, but still this is a mistake).

I think this is completely a design choice (since the whole point of B is to support sorted data), and I would not say that this is a mistake, especially without reporting a problem or any information about the expected behavior. This does not even violate the idea or concept of polymorphism.

Could you identify it as a mistake?

+5
source share
1 answer

We could decide that it is part of the class A interface and, more specifically, the post-state A::getValue , that A::getValue should return the same value that was (immediately) previously assigned to A::setValue at the same index .

If such a message condition is specified, then a derived class that violates the message condition will violate the Liskov replacement principle . This will be a design mistake.


The exercise does not indicate whether such a post-state exists or should exist. And without a message condition, there is no design error (at least not the error I am discussing).

Whether or not a message state supposedly assumed by your interviewer should exist depends on the hierarchy constructor. Which in your interview was you. Whether the post condition is a good design for a hierarchy will depend on what models model. If A models something like std::array - and that's what it looks like - then the message condition is certainly a good design choice, in my opinion.

+9
source

Source: https://habr.com/ru/post/1262944/


All Articles