3 ms·
There are various answers which seem a bit sloppy to me. For example, "How do you find the middle element of a singly linked list in one pass?" The answer, wi
by jfultz 8y ago
There are various answers which seem a bit sloppy to me. For example, "How do you find the middle element of a singly linked list in one pass?" The answer, without comment, makes a presumption about how to define the "middle" of a list with even length. And it defines a linked list implementation with a magic, invisible head that requires extra code beyond the loop to compensate for. In the "How do you check if two rectangles overlap with each other?" answer, the code will determine that the rectangles which merely coincide at edges or corners are overlapping, without noting that distinction. The vending machine example tries to drive home the point of requirements definition, but the point applies equally to these much simpler examples. I only gleaned maybe 10 examples...maybe I just got "lucky".
Also, the overlapping rectangles example has an error in the explanation. It says "If any of above four conditions is not true then two rectangles are overlapping with each other, as in following diagram first condition is violated, hence rectangle A intersects rectangle B." This somewhat nonsensical statement refers to a diagram that's not there, but furthermore, incorrectly indicates that the rectangles overlap if "any of [the] conditions is not true", when in fact all of them must be false. The code is correct on this point, but the explanation is wrong.
- thaumasiotes 8y ago> For example, "How do you find the middle element of a singly linked list in one pass?" The answer, without comment, makes a presumption about how to define the "middle" of a list with even length. And it defines a linked list implementation with a magic, invisible head that requires extra code beyond the loop to compensate for. I'm not sure what you mean by complaining about a magic, invisible head to the linked list. If you can't access the head of a linked list, you can't access any other part of it either. However, as another comment points out elsewhere in the thread, there is a bigger problem -- the solution offered uses more than one pass.
- jfultz 8y agoI mean that the LinkedList class is defined so that "new LinkedList()" returns not a list of length zero, but a list of length 1 with the first element containing the string "head". The actual list it creates is {"head", "1", "2", "3", "4"}, despite the fact that the output claims the list has 4 elements.