Interview question: a list with 10000 numbers to each number in list + 1. How to realize

I answered using the map function + 1.
in java8"s stream class, but watching the interviewer"s reaction convinced me that this was not the right answer. What would you do

Mar.09,2021

Stream of

Java8 can be executed concurrently, but Stream does not change that the original list, can only return a new list, and assign a value to the reference of the original list. But if the list is RandomAccess , that is, the underlying implementation is an array, such as ArrayList , then you can directly use the traditional for loop to traverse, because for RandomAccess List , the time complexity of accessing the array elements through the subscript is O (1), then the time complexity of traversing is O (N), which is an excellent time complexity, and no extra space is used. The space complexity is O (1);
if not, for example, LinkedList , then the time complexity of the corresponding element in the list is obtained by subscript. If O (N), uses the previous method, then the total time complexity will be O (N ^ 2), then it is recommended to create a new List of the same size, and then traverse the original list, to add the value of + 1 for each element to the new List . At this time, the time complexity is O (N), and the space complexity is also O (N). (of course, you can also use Stream to generate a new List )

. < hr >

so I guess the interviewer is not satisfied with your answer because you don't take into account different List types.

Menu