1
00:00:01,010 --> 00:00:02,630
Let's do this question.

2
00:00:02,660 --> 00:00:09,410
You are given an array of integers in which each subsequent value is not less than the previous value.

3
00:00:09,440 --> 00:00:15,710
Write a function that takes this array as an input, and returns a new array with the squares of each

4
00:00:15,710 --> 00:00:18,110
number sorted in ascending order.

5
00:00:18,140 --> 00:00:20,210
Now let's try to understand this question.

6
00:00:20,210 --> 00:00:27,770
We will be given an array of integers, and each subsequent value is not less than the previous value.

7
00:00:27,800 --> 00:00:31,940
That means every subsequent value can be either equal or greater.

8
00:00:31,940 --> 00:00:38,240
So basically we are given an array of integers that's sorted in ascending order, and the output of

9
00:00:38,240 --> 00:00:45,560
the function should be an array where the squares of each number is sorted in ascending order, is what's

10
00:00:45,560 --> 00:00:46,520
contained in the new array.

11
00:00:46,520 --> 00:00:47,570
So this is the question.

12
00:00:47,570 --> 00:00:53,720
Now when you are given such a question in the interview, always feel free to ask clarifying questions.

13
00:00:53,720 --> 00:00:59,300
So this is very important so that you and the interviewer are on the same page that you're very clear

14
00:00:59,300 --> 00:01:00,620
with the expected output.

15
00:01:00,620 --> 00:01:05,360
Now a possible question that you could ask is are all numbers positive over here?

16
00:01:05,360 --> 00:01:06,980
It's mentioned an array of integers.

17
00:01:06,980 --> 00:01:13,490
Right now at this point let's say that the interviewer replies no, there can be negative numbers and

18
00:01:13,490 --> 00:01:15,950
there can be zero also as an integer.

19
00:01:16,010 --> 00:01:16,490
All right.

20
00:01:16,520 --> 00:01:20,150
Now another question could be are all the integers distinct?

21
00:01:20,540 --> 00:01:23,180
Now a possible answer can be not necessarily.

22
00:01:23,180 --> 00:01:24,710
So that's not required.

23
00:01:24,710 --> 00:01:29,360
And another question could be can an empty array of integers be given as an input?

24
00:01:29,360 --> 00:01:33,380
Now let's say the interviewer replies and says yes, that can be possible.

25
00:01:33,380 --> 00:01:37,280
In that case, return an empty array as the output of the function.

26
00:01:37,280 --> 00:01:37,880
All right.

27
00:01:37,880 --> 00:01:43,130
So now we have understood the question and we have also asked clarifying questions.

28
00:01:43,130 --> 00:01:47,870
And the interviewer and the person who is giving the interview are on the same page.

29
00:01:47,870 --> 00:01:53,630
You have clarity on what the question asks and what the expected output is in, especially in the case

30
00:01:53,630 --> 00:01:54,710
of edge cases.

31
00:01:54,710 --> 00:01:55,310
All right.

32
00:01:55,310 --> 00:02:01,790
So as a next step to understand further and to formulate a logic in our mind, it's always helpful to

33
00:02:01,790 --> 00:02:03,980
write down different test cases.

34
00:02:03,980 --> 00:02:06,080
So test cases are different inputs.

35
00:02:06,080 --> 00:02:10,340
And what the expected output is in all those particular cases.

36
00:02:10,340 --> 00:02:13,850
And when you're forming test cases you can work with the interviewer.

37
00:02:13,850 --> 00:02:20,210
You can ask the interviewer, is it okay if we come up together with a few test cases based on the criterias

38
00:02:20,210 --> 00:02:21,200
that we have discussed.

39
00:02:21,200 --> 00:02:23,420
So you can always do that, and that's helpful.

40
00:02:23,420 --> 00:02:26,630
Now let's form a few test cases for the question at hand.

41
00:02:26,630 --> 00:02:33,140
Let's say the input is an array where one, three and five are the, uh integers in the array.

42
00:02:33,170 --> 00:02:36,140
Now we would have to square these and sort them right.

43
00:02:36,140 --> 00:02:40,580
So one square is one, three square is nine and five square is 25.

44
00:02:40,580 --> 00:02:43,880
So the output should be one nine and 25.

45
00:02:44,120 --> 00:02:48,080
Now let's say we have another array which is zero five and six.

46
00:02:48,080 --> 00:02:50,210
So what should be the output in this case.

47
00:02:50,210 --> 00:02:54,380
In this case the output should be zero 25 and 36.

48
00:02:54,380 --> 00:02:58,160
Right 25 is five square and 36 is six square.

49
00:02:58,190 --> 00:03:04,310
Now over here in these two cases, an interesting thing is that the position of the square has not changed.

50
00:03:04,310 --> 00:03:04,580
Right.

51
00:03:04,580 --> 00:03:05,930
So this is the first.

52
00:03:05,930 --> 00:03:09,290
And again one square is also at the first in the output array.

53
00:03:09,290 --> 00:03:13,190
And you have three at index let's say 012.

54
00:03:13,190 --> 00:03:15,140
And these are the indices 012.

55
00:03:15,170 --> 00:03:16,850
So at index one you have three.

56
00:03:16,850 --> 00:03:19,820
And at index one itself you have three square which is nine.

57
00:03:19,820 --> 00:03:22,070
And at index two we have five.

58
00:03:22,070 --> 00:03:25,010
And at index two we have five square which is 25.

59
00:03:25,010 --> 00:03:27,920
So in these two cases this can be observed.

60
00:03:27,920 --> 00:03:34,880
But let's say we have an array with negative and positive integers like minus four minus two 013.

61
00:03:34,880 --> 00:03:36,470
Now what's happening over here.

62
00:03:36,920 --> 00:03:40,760
First let's square these individual integers.

63
00:03:40,760 --> 00:03:47,210
So minus four square is 16 minus two square is four, zero square is zero, one square is one and three

64
00:03:47,210 --> 00:03:48,110
square is nine.

65
00:03:48,110 --> 00:03:52,340
Right now in the output the these have to be sorted right.

66
00:03:52,340 --> 00:03:56,480
So the output would be zero, one, four, nine and 16.

67
00:03:56,480 --> 00:03:58,760
Now we can see that the position has changed.

68
00:03:58,760 --> 00:04:02,180
This is 0123 and four indices.

69
00:04:02,180 --> 00:04:04,910
And over here let's look at the indices.

70
00:04:04,910 --> 00:04:09,110
We can see that here minus four was at index zero.

71
00:04:09,110 --> 00:04:13,070
But minus four square which is 16 is at index four over here.

72
00:04:13,070 --> 00:04:16,370
So this also has to be taken care by the algorithm.

73
00:04:16,370 --> 00:04:22,850
And again if we have same values like for example we have uh two and three comma three.

74
00:04:22,850 --> 00:04:26,450
So the output would be four nine comma nine.

75
00:04:26,450 --> 00:04:31,280
So that's also possible as per the questions that we have discussed with the interviewer.

76
00:04:31,280 --> 00:04:31,940
All right.

77
00:04:31,940 --> 00:04:37,640
Now in the next video let's go ahead and discuss how we can solve this.

78
00:04:37,640 --> 00:04:43,970
And let's come up with a working solution, uh, just based on logic without looking at the code of

79
00:04:43,970 --> 00:04:44,810
this solution.
