Let's do this question.
You are given an array of integers in which each subsequent value is not less than the previous value.
Write a function that takes this array as an input, and returns a new array with the squares of each
number sorted in ascending order.
Now let's try to understand this question.
We will be given an array of integers, and each subsequent value is not less than the previous value.
That means every subsequent value can be either equal or greater.
So basically we are given an array of integers that's sorted in ascending order, and the output of
the function should be an array where the squares of each number is sorted in ascending order, is what's
contained in the new array.
So this is the question.
Now when you are given such a question in the interview, always feel free to ask clarifying questions.
So this is very important so that you and the interviewer are on the same page that you're very clear
with the expected output.
Now a possible question that you could ask is are all numbers positive over here?
It's mentioned an array of integers.
Right now at this point let's say that the interviewer replies no, there can be negative numbers and
there can be zero also as an integer.
All right.
Now another question could be are all the integers distinct?
Now a possible answer can be not necessarily.
So that's not required.
And another question could be can an empty array of integers be given as an input?
Now let's say the interviewer replies and says yes, that can be possible.
In that case, return an empty array as the output of the function.
All right.
So now we have understood the question and we have also asked clarifying questions.
And the interviewer and the person who is giving the interview are on the same page.
You have clarity on what the question asks and what the expected output is in, especially in the case
of edge cases.
All right.
So as a next step to understand further and to formulate a logic in our mind, it's always helpful to
write down different test cases.
So test cases are different inputs.
And what the expected output is in all those particular cases.
And when you're forming test cases you can work with the interviewer.
You can ask the interviewer, is it okay if we come up together with a few test cases based on the criterias
that we have discussed.
So you can always do that, and that's helpful.
Now let's form a few test cases for the question at hand.
Let's say the input is an array where one, three and five are the, uh integers in the array.
Now we would have to square these and sort them right.
So one square is one, three square is nine and five square is 25.
So the output should be one nine and 25.
Now let's say we have another array which is zero five and six.
So what should be the output in this case.
In this case the output should be zero 25 and 36.
Right 25 is five square and 36 is six square.
Now over here in these two cases, an interesting thing is that the position of the square has not changed.
Right.
So this is the first.
And again one square is also at the first in the output array.
And you have three at index let's say 012.
And these are the indices 012.
So at index one you have three.
And at index one itself you have three square which is nine.
And at index two we have five.
And at index two we have five square which is 25.
So in these two cases this can be observed.
But let's say we have an array with negative and positive integers like minus four minus two 013.
Now what's happening over here.
First let's square these individual integers.
So minus four square is 16 minus two square is four, zero square is zero, one square is one and three
square is nine.
Right now in the output the these have to be sorted right.
So the output would be zero, one, four, nine and 16.
Now we can see that the position has changed.
This is 0123 and four indices.
And over here let's look at the indices.
We can see that here minus four was at index zero.
But minus four square which is 16 is at index four over here.
So this also has to be taken care by the algorithm.
And again if we have same values like for example we have uh two and three comma three.
So the output would be four nine comma nine.
So that's also possible as per the questions that we have discussed with the interviewer.
All right.
Now in the next video let's go ahead and discuss how we can solve this.
And let's come up with a working solution, uh, just based on logic without looking at the code of
this solution.