First check if it is an Array or List of numbers/integers, as you are supposed to provide the
code for that.
Check if you are supposed to solve this problem using Functional way or Imperative style.
Though chances are very high, as you are supposed to write the code for such a simple
problem, then the interviewer is looking your functional way.
It is to be written using Functional way & if interviewer says you can take anything List or
Array. Then go with List & solution will be like shown below-
code for that.
Check if you are supposed to solve this problem using Functional way or Imperative style.
Though chances are very high, as you are supposed to write the code for such a simple
problem, then the interviewer is looking your functional way.
It is to be written using Functional way & if interviewer says you can take anything List or
Array. Then go with List & solution will be like shown below-
Using List
Now if interviewer says that it is array of numbers, then surely that interviewer
has recently worked on such scenario or seen on internet while preparing for the
nterviews.
As if you are supposed to give the sample using an array, then be careful while declaring
such array. Why?
If you declare your array as int[] array, then it needs to be handled in different way.
So you will need to convert this array to Stream.
We have 2 ways for this-
a) Use Arrays.stream()
b) Use Stream.of();
What's the issue then?
Lets have such array like : int[] premNums = {1,2,3,4,5,6,7,8,9,10};
Using (a) : IntStream intStream = Arrays.stream(premNums);
Here we get IntStream, & looking this seems no issue. I also thought the same but I didn't
get the way to solve the problem using this IntStream, if anyone knows please comment.
Using (b) : Stream<int[]> arrayStream = Stream.of(premNums);
Here we see the problem now directly, as we need the stream of numbers but here we
are getting stream of int[].
Here it becomes bit tricky to get this stream like the way we need.
Below is the way & note the use of boxed() in flatMap()
has recently worked on such scenario or seen on internet while preparing for the
nterviews.
As if you are supposed to give the sample using an array, then be careful while declaring
such array. Why?
If you declare your array as int[] array, then it needs to be handled in different way.
So you will need to convert this array to Stream.
We have 2 ways for this-
a) Use Arrays.stream()
b) Use Stream.of();
What's the issue then?
Lets have such array like : int[] premNums = {1,2,3,4,5,6,7,8,9,10};
Using (a) : IntStream intStream = Arrays.stream(premNums);
Here we get IntStream, & looking this seems no issue. I also thought the same but I didn't
get the way to solve the problem using this IntStream, if anyone knows please comment.
Using (b) : Stream<int[]> arrayStream = Stream.of(premNums);
Here we see the problem now directly, as we need the stream of numbers but here we
are getting stream of int[].
Here it becomes bit tricky to get this stream like the way we need.
Below is the way & note the use of boxed() in flatMap()
Using array of primitive integers
Now if we declare our array of numbers as : Integer[] nums = {1,2,3,4,5,6,7,8,9,10};
So here this makes the life bit easy & like List of numbers. Why?
Because now if we convert this array to Stream using either (a) or (b) as suggested above, then in both the cases we get stream of Integer, not stream of int[], like shown in below code-
So here this makes the life bit easy & like List of numbers. Why?
Because now if we convert this array to Stream using either (a) or (b) as suggested above, then in both the cases we get stream of Integer, not stream of int[], like shown in below code-
Using array of objects or wrapper types