Array list algorithm - Interview

雨燕双飞 提交于 2020-01-01 08:45:17

问题


I was asked this question in an interview today. I have tried a solution but would like to know if there's a better way to solve this:

Question: I have an arraylist say of 500,000 elements such that the value of each element of the arraylist is same as the index. For ex: list.get(0) = 0; list.get(1) = 1 ...etc. But only one element is out of sync with this ordering [i.e list.get(i) != i]. How do you find that element.

My Answer: Iterate over the list using multiple threads each thread handling a certain splice of the arraylist each time comparing list.get(i) with i. When the element is found, set some boolean variable to indicate to other threads that the element has been found.

Is there a way to solve this problem without iterating over the list? Or a better way?


回答1:


In the worst case you have to examine each element, so you can't improve on the O(n) time complexity.

With this in mind, the best algorithm is to scan the array list from start to finish. This way you're making best use of the available memory bandwidth.

It is not entirely clear to me how or why threading has entered the picture. It seems out of place. Was it part of the question?




回答2:


The answer is: one iteration. Your mention of concurrency of cause is something they are fishing for.

In fact since java 8, the solution whether parallel or not is simple. I think the most would have brought:

OptionalInt foundInt = IntStream.range(0, list.size())
    .parallelStream()
    .filter(i -> i != list.get(i))
    .findAny();



回答3:


You can't do better than O(n).

And secondly, I think it's a bad idea to talk about threads and multithreading in those problems. They are not of interest at all. In the end you have a runtime of O(whatever) where your constant is removed anyway.

Maybe the interviewer meant a sorted array with elements from 0 to n-1 with index 0 to n-1. And then move one element to a different position. But that means that all the remaining elements have different indexes! In this scenario you can improve your search with binary search:

Then you can get the element in O(log n). Start in the middle and check whether the index equals the element. If it is equal do the same with the upper part of the half, if not use the other part.




回答4:


Further to @aix's answer, how about doing 2 checks per loop:

for (int i = 0; i < list.size / 2; i++)
{
  if (i != list.get(i))
  {
    return i;
  }
  else if (list.size - i != list.get(list.size - i)
  {
    return list.size - i;
  }
}



回答5:


1. iterate through the list 
2. check for the condition in the elements
3. when that only element found break out the loop... 

I Don't think Thread Enters the Arena...




回答6:


ArrayList<Integer> s = new ArrayList<Integer>();

for (int i=0; i<500000; i++) {
    s.add(i);
}

s.set(13, 500002);

for (int j=0; j<s.size(); j++) {
    if (j != s.get(j)) {
        System.out.println(j + " " + s.get(j));
    }   
}


来源:https://stackoverflow.com/questions/10335180/array-list-algorithm-interview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!