Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:06,600
So I hope you've given it a try and you've managed to solve this interview question on your own.
2
00:00:06,780 --> 00:00:12,560
Even so, and even if not, let's do it together to make sure that you've done it right.
3
00:00:12,870 --> 00:00:21,780
So our task is to make a swap between variable A and variable B without using any additional variables,
4
00:00:21,810 --> 00:00:24,220
without using some temp variable or.
5
00:00:24,250 --> 00:00:24,610
Right.
6
00:00:24,810 --> 00:00:29,310
And to do so, there are several approaches that basically we can use.
7
00:00:29,610 --> 00:00:33,300
Each of them involves some math operations.
8
00:00:33,930 --> 00:00:39,390
Well, I mean, basically, how can we do anything without using math?
9
00:00:39,480 --> 00:00:39,930
Right.
10
00:00:40,080 --> 00:00:45,510
And this solution simply involves some addition and subtraction operations.
11
00:00:45,900 --> 00:00:48,420
Only the plus and minus operations.
12
00:00:48,420 --> 00:00:54,480
Write something that you know how to do, plus and minus how to use additions and how to use substraction,
13
00:00:54,570 --> 00:01:02,100
subtraction and all that we are going to do here in this solution may seem to you at first glance like
14
00:01:02,370 --> 00:01:03,420
sort of a trick.
15
00:01:03,590 --> 00:01:05,670
Okay, but that's not a trick.
16
00:01:05,850 --> 00:01:12,150
We just use math rules to make assumptions about the result of different operations.
17
00:01:12,300 --> 00:01:16,980
And with that being said, let's move on to our first step.
18
00:01:17,160 --> 00:01:24,600
But beforehand, we will give track some reminder of what were the values of A and B before swapping.
19
00:01:24,750 --> 00:01:30,930
So basically, we will simply not get lost with what was the value of A and what was the value of B
20
00:01:31,170 --> 00:01:35,370
before the actual swapping before we begin all of these functionality.
21
00:01:35,520 --> 00:01:41,940
So we will say that initial A equals two 10 and initial B equals two 20.
22
00:01:42,560 --> 00:01:49,980
And now let's put in variable A, the sum of what is already there, which is actually the ace value,
23
00:01:49,980 --> 00:01:55,220
which is stem and add to the value of B, which is 20.
24
00:01:55,410 --> 00:02:02,400
This means that after these first operation, these first addition we will have inside of variable a
25
00:02:02,550 --> 00:02:06,450
Vinicio, a value plus the initial B value.
26
00:02:06,450 --> 00:02:06,750
Right.
27
00:02:06,780 --> 00:02:10,020
That what we've done here, we just use the addition operation.
28
00:02:10,170 --> 00:02:18,720
So simply saying now variable A will have the value of thirty and variable B still remain unchanged.
29
00:02:18,960 --> 00:02:19,290
Right.
30
00:02:19,320 --> 00:02:21,390
We didn't make any assignments to it yet.
31
00:02:21,660 --> 00:02:25,650
So it's just remains with his value of twenty.
32
00:02:25,800 --> 00:02:26,160
All right.
33
00:02:26,200 --> 00:02:27,810
If these the initial B.
34
00:02:27,930 --> 00:02:33,250
So to summarize the first step, what we've done so far, what we have now, our current status.
35
00:02:33,300 --> 00:02:33,650
OK.
36
00:02:33,960 --> 00:02:41,520
We know that in the variable A. We currently have both the initial A value and the initial B value.
37
00:02:41,820 --> 00:02:48,390
And also in variable B, we currently have the initial B value is very clear so far.
38
00:02:48,480 --> 00:02:49,570
This is the first step.
39
00:02:49,650 --> 00:02:51,030
I think it's pretty easy.
40
00:02:51,330 --> 00:02:55,710
We've just done some basic math operations and we are ready to move on.
41
00:02:55,920 --> 00:02:56,200
Oh.
42
00:02:56,220 --> 00:03:02,790
And just don't forget to update the value of failure like we've written here below, which is 30 in
43
00:03:02,790 --> 00:03:03,300
this case.
44
00:03:03,660 --> 00:03:08,400
And now we are ready to move on to the second step, which is subtraction.
45
00:03:08,490 --> 00:03:15,750
So our goal is to put in variable B, the actual previous the actual initial value of A.
46
00:03:15,990 --> 00:03:17,310
And vice versa.
47
00:03:17,340 --> 00:03:17,670
Right.
48
00:03:18,180 --> 00:03:21,890
And now we can easily get that just by using subtraction.
49
00:03:21,900 --> 00:03:25,900
We can say that B equals to A minus B.
50
00:03:26,370 --> 00:03:27,930
This can be viewed as the following.
51
00:03:28,050 --> 00:03:28,290
Right.
52
00:03:28,320 --> 00:03:30,200
We know that A at this point.
53
00:03:30,210 --> 00:03:31,300
At this current point.
54
00:03:32,100 --> 00:03:36,090
It's the initial A plus the initial B we can see here on the left.
55
00:03:36,300 --> 00:03:43,440
And we just instead of using A, we specify that this is the initial A plus initial B minus B, which
56
00:03:43,440 --> 00:03:46,530
B in this case currently is just the initial B.
57
00:03:46,680 --> 00:03:52,880
So we if we make these subtraction, we will see that ten plus 20, minus 20 equals to 10.
58
00:03:53,040 --> 00:03:53,370
Right.
59
00:03:53,670 --> 00:04:00,990
So now B, B, the variable B has the actual previous A value of the initially A value.
60
00:04:01,020 --> 00:04:06,480
We can see that B has the initial A value, which is ten.
61
00:04:06,630 --> 00:04:12,360
So that's the summary of the second step of the second step, which is subtraction.
62
00:04:12,690 --> 00:04:16,180
And we see that the current status of A remains the same.
63
00:04:16,200 --> 00:04:20,360
It's initially plus initial B and B equals two initial A.
64
00:04:20,700 --> 00:04:27,360
So now we can see the third that we can take a look at the third step, which is also using a subtraction.
65
00:04:27,870 --> 00:04:34,030
So variable B at this point already has the value it should have after the swap operation.
66
00:04:34,050 --> 00:04:34,350
Right.
67
00:04:34,590 --> 00:04:37,170
It already has the initial a value.
68
00:04:37,200 --> 00:04:39,180
So we know that it's OK.
69
00:04:39,270 --> 00:04:41,490
B is looking pretty much good.
70
00:04:41,850 --> 00:04:44,370
We can say that we are in the right direction.
71
00:04:44,520 --> 00:04:51,780
But we do remember that our task is also that variable A will have the initial B value.
72
00:04:51,800 --> 00:04:52,170
Right.
73
00:04:52,440 --> 00:04:54,990
And currently that is not the case yet.
74
00:04:55,260 --> 00:04:58,500
So our task is to remove.
75
00:04:58,650 --> 00:04:59,000
Right.
76
00:04:59,040 --> 00:04:59,690
Remove these.
77
00:04:59,900 --> 00:05:06,390
Michelle A. from the some of initially blessing Michelle B, which is an inside of variable A and to
78
00:05:06,390 --> 00:05:09,570
remain with just the initial B value.
79
00:05:09,930 --> 00:05:16,410
So for that, we are going to use the same step prefigures pretty much the same step of using subtraction
80
00:05:16,410 --> 00:05:18,090
as you've seen in the second step.
81
00:05:18,420 --> 00:05:25,560
So you go like a equals to A minus B, so you just replace the value of A in this equation.
82
00:05:25,590 --> 00:05:31,050
You say that A equals two initially plus initial B, which you can see that it's the actual value of
83
00:05:31,050 --> 00:05:36,390
Fey at this point minus the initial eight, which is the value of variable B.
84
00:05:36,600 --> 00:05:43,770
Now if you just put it here, the real value is that you can see there reminder here on the bottom left
85
00:05:43,770 --> 00:05:45,690
part of this slide.
86
00:05:46,170 --> 00:05:51,580
You can see that A equals to ten plus twenty minus ten, which is a total of twenty.
87
00:05:51,600 --> 00:06:00,510
So the initial A is being canceled and the variable A remains only with the value of initial B, which
88
00:06:00,510 --> 00:06:01,260
is 20.
89
00:06:01,320 --> 00:06:09,770
In our example, in basically you can see that just by using these three steps, you've completed successfully
90
00:06:09,790 --> 00:06:12,800
the swap operation between two variables.
91
00:06:12,810 --> 00:06:13,130
Right.
92
00:06:13,320 --> 00:06:19,590
You can see that, first of all, the initial a value was ten and the initial B value was 20.
93
00:06:19,710 --> 00:06:26,380
And now after the swap operation, after these three steps that you've done by using only additions
94
00:06:26,380 --> 00:06:32,310
and two subtractions, you've managed to swap the values between variable A and B.
95
00:06:32,460 --> 00:06:37,400
And it's not some sort of trick that works only for values 10 and 20.
96
00:06:37,740 --> 00:06:39,840
You can use it for any value, right?
97
00:06:39,870 --> 00:06:45,940
Because it's just like in math we use here at two variables, let's say X and Y, which were just the
98
00:06:45,980 --> 00:06:51,510
initially and the initial B were simply done, some equations, some mathematical operations.
99
00:06:51,750 --> 00:06:55,760
And we've managed to solve this one on on our wrong right.
100
00:06:55,920 --> 00:07:02,370
So compare your results and what you've done so far with what I've done so far are there are also,
101
00:07:02,370 --> 00:07:09,660
as I've said previously, additional ways to solve this exercise by using multiplication division,
102
00:07:09,690 --> 00:07:11,280
bitwise operations and so on.
103
00:07:11,580 --> 00:07:15,390
But we are going to stick with this solution because it's the easiest.
104
00:07:15,420 --> 00:07:22,320
And I think that you can manage to solve it on your interview questions and to let your interviewer
105
00:07:22,320 --> 00:07:28,290
see that you really know how and even that these sort of things can be done.
106
00:07:28,500 --> 00:07:34,620
I mean, sometimes in the pressure of your interview, you're interviewing me may even ask you, is
107
00:07:34,620 --> 00:07:35,790
it even possible?
108
00:07:35,880 --> 00:07:37,500
And you're going to see.
109
00:07:37,890 --> 00:07:38,940
I don't know.
110
00:07:39,030 --> 00:07:41,520
We always done it with the third variable.
111
00:07:41,520 --> 00:07:43,060
I don't know if it's impossible.
112
00:07:43,410 --> 00:07:44,350
You may have an answer.
113
00:07:44,390 --> 00:07:45,510
No, it's not possible.
114
00:07:45,540 --> 00:07:47,370
But you see that the.
115
00:07:47,460 --> 00:07:48,600
That's not the answer.
116
00:07:48,960 --> 00:07:52,830
The real answer, that it can be done and it can be done in many ways.
117
00:07:53,070 --> 00:07:59,580
And one of them is very, very simple, just using addition and subtraction operations.
118
00:07:59,760 --> 00:08:01,290
So I hope that's clear.
119
00:08:01,330 --> 00:08:04,090
Guys, you really are amazing.
120
00:08:04,260 --> 00:08:08,160
And I want to thank you for on joining this course.
121
00:08:08,280 --> 00:08:11,670
Getting to this point, making these interview question.
122
00:08:11,820 --> 00:08:19,590
Don't forget to leave some feedback and hopefully I will see you in another videos like this one.
123
00:08:19,710 --> 00:08:24,000
And I wish you good luck on your interviews and your exams and have a great day.
124
00:08:24,300 --> 00:08:24,780
Goodbye.
11731
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.