File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Remove elements from a list using array indexes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove elements from a list using array indexes" Watch "Remove elements from a list using array indexes" New topic
Author

Remove elements from a list using array indexes

santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Hi, I have an issue.
Imagine a method which takes two parameters a list and an array, which contains indexes of elements to be removed from the list.
I have come with a solution to the problem, i will paste the code here.
This solution is working, if any of you see any problem with this one, kindly mention.
If any of you have a better solution please indicate. Thanks.

santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
This question was asked in an interview, and this was my solution. But the interviewer didn't agree with my solution, so if any of you have a better solution please indicate and also problems issues with this one.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Don't just look att the size of the result. Look at the contents of the resulting list. What values remain in the list? Which values were removed? Are they they ones you expected to be removed?

You might want to try replacing

with

Do you get the same result in each case? Should you get the same result in each case?
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

santosh kumar sharma wrote:This question was asked in an interview, and this was my solution. But the interviewer didn't agree with my solution, so if any of you have a better solution please indicate and also problems issues with this one.

Suppose you need to remove 1st and 3rd elements from the list,
if you remove the 1st element, the 3rd element becomes the 2nd element in the new list.

Would that be the reason why the interviewer don't like your solution ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, I was trying to be more subtle and get the original poster to notice that himself. ;)
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Mike Simmons wrote:Don't just look att the size of the result. Look at the contents of the resulting list. What values remain in the list? Which values were removed? Are they they ones you expected to be removed?

You might want to try replacing

with

Do you get the same result in each case? Should you get the same result in each case?


Thanks Mike for pointing out this anomaly. But i can insert a single line to correct this.
Arrays.sort(arr);
before the remove operation.
This can't be a deal breaker for the interviewer to reject my solution.
Thanks for pointing out, but could you indicate more problems, and any other better solution.
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Raymond Tong wrote:
santosh kumar sharma wrote:This question was asked in an interview, and this was my solution. But the interviewer didn't agree with my solution, so if any of you have a better solution please indicate and also problems issues with this one.

Suppose you need to remove 1st and 3rd elements from the list,
if you remove the 1st element, the 3rd element becomes the 2nd element in the new list.

Would that be the reason why the interviewer don't like your solution ?



You are aright in indicating that this a list and the remove operation will shift the elements, that's why i am doing arr[i] - i, in a loop. After the first delete the later remove operation will have their indexes subtracted by one.

And the interviewer never told me why he wasn't happy with my solution, except that this is complex and will not work, You can try at home. I tried and it works.
So i wanted to know if there is a better way that the knowledgeable guys in the forum can suggest.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

How about this ?
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
I think he was expecting following steps for a complete solution
1) Sorting it first
2) Check the validity of indexes
3) Decrementing all except the first element in the index array with respect to
number of elements preceding it & whether the indexes in array are 0-based or 1-based
4) Then perform a simple loop to remove the given indexes.


Runtime exception IndexOutOfBounds

and

improper index (are these element at 0,1,2,3,... or 1st ,2nd,3rd,... ) interpretation could be your two faling areas.




Regards,
Avishkar Nikale
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Raymond Tong wrote:How about this ?


There's a problem with this removeAll method.
First it takes a collection and not an array.
If you try to convert the array to list using Arrays.asList(arr), then the list contains indexes and it will do nothing.
If you create a fresh list containing the index values and use the removeAll method, then what if the element is being repeated. Suppose at the 4th index you have value twenty which you want to remove. The value twenty is in the list at the 2nd index also. When you try to remove using the array values instead of array values you can run in trouble.
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Avishkar Nikale wrote:I think he was expecting following steps for a complete solution
1) Sorting it first
2) Check the validity of indexes
3) Decrementing all except the first element in the index array with respect to
number of elements preceding it & whether the indexes in array are 0-based or 1-based
4) Then perform a simple loop to remove the given indexes.




Hi avishkar, you are right in pointing out that. But he gave me a custom situation with array indexes like {1,3,5,9}. And all were ordered, so the sorting never came to my mind. But if he mentioned this problem, i would have solved it at that stage itself.
He just mentioned its complex, will not woork. You should try it at home.
So i wanted to know if there is a better, simpler solution than this.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Depends on any assumption, if you have to deal with concurrency, it could be more complex.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Hi Santosh,

Did he at any point say that
these array elements are 0-based or 1-based.

Also you are not checking any validity for the index which is more than enough for him to reject the solution.

Again it is a matter of opinion. I could figure out these two issues due to which he could have not accepted
the solution.

A simple advice, if your interviewer does not divulge information needed
just ask him/her for it & reconfirm while explaining your code & say I would have done this if the case changed.
Never leave anything to assumption even if he/she says its a custom situation.

Better safe than sorry.

Thanks for posting & best of luck.
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Avishkar Nikale wrote:Hi Santosh,

Did he at any point say that
these array elements are 0-based or 1-based.

Also you are not checking any validity for the index which is more than enough for him to reject the solution.

Again it is a matter of opinion. I could figure out these two issues due to which he could have not accepted
the solution.

A simple advice, if your interviewer does not divulge information needed
just ask him/her for it & reconfirm while explaining your code & say I would have done this if the case changed.
Never leave anything to assumption even if he/she says its a custom situation.

Better safe than sorry.

Thanks for posting & best of luck.


Thanks for pointing this out. I will be careful in future regarding assumptions.
But i still feel the motive of question was more about the algorithm than the finer prints of algorithm.
Still it's a valuable advice.
santosh kumar sharma
Greenhorn

Joined: Apr 05, 2010
Posts: 10
Raymond Tong wrote:Depends on any assumption, if you have to deal with concurrency, it could be more complex.


Raymond you maybe right. But could you show me a custom example to better help me understand. thanks
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

santosh kumar sharma wrote:
Raymond Tong wrote:Depends on any assumption, if you have to deal with concurrency, it could be more complex.


Raymond you maybe right. But could you show me a custom example to better help me understand. thanks

I think your question wasn't that complicated but I just quote concurrency as an example.
Suppose you checked the index of removal is valid.
There is another thread remove an element from the list.
The index of removal is invalid now and calling List.remove would throw IndexOutOfBoundException.

I agree on Avishkar's view, ask the interviewer to confirm would be better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove elements from a list using array indexes