Adobe Interview Question
Software Engineer / Developerslist can not be accessed by directly by indexes.this is a drawback of lists over other sequence containers. So list(i) is not a valid way to access.
Not mentioning the syntactical rules, the problem lies in the algorithm. the algorithm wants to check all the nodes inside the list but it will not be able to in some conditions
The list nodes are being deleted inside a for loop which runs for the length of the list. this will create problems and may not iterated over the entire list which would be a bug.
for example, assume that the list has 5 nodes, so the list length is 5. let the iteration counter be equal to 3 (i=3) so that the 4th node is being checked, let it satisfy the condition and be removed so that the list length now is equal to 4. however, 'i' is 3 in current iteration. after removal, i would be incremented to 4.
now, we have only compared the 4th node with i=3, deleted it and incremented i to 4. length is also 4 since we have removed a node from the list. so now, when the condition in for loop is checked again, it says that 'i' should be less that length of list. since both are equal to 4, the flow will exit from the for loop and the last node of the list will not be checked.
The best idea is to run inside a while loop and see if the current node is the last node of the list.
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
You should not alter the list while iterating through it.
- Anonymous August 04, 2010